user445714
user445714

Reputation: 393

Threads from a class with constructor with multiple arguments

Having a problem here, I have been learning about threading in java i understand if you are extending a thread you would create a thread in the main as follows.

Card thread1 = new Card("Ace");

This would be coming from a class called thread with a constructor

public thread(String n);

But i have given the constructor with a multiple argument:

 public Person(int PersonID, Direction direction, StairLock stairLock)

And asked to create a thread for each of 4 people with two people going UP and 2 going DOWN and a lock for the stair, im unsure how to do this any help or direction would be helpful

Upvotes: 1

Views: 4928

Answers (3)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298878

Thread vs Runnable

You should probably not extend thread.

Implement Runnable and start a new thread with your Runnable.

Reason: If you extend Thread, you interfere with working infrastructure code. You should only do that if you need to change how things work. In 90% of cases, you only need to specify what needs to be executed, not how. That's why the Runnable approach is more elegant: you separate the what from the how.

From the Runnable docs:

In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Sample Code

Try something like this:

public void startThread(final int personID,
    final Direction direction,
    final StairLock stairLock){
    new Thread(new Runnable(){

        @Override
        public void run(){
            System.out.println("Person " + personID + " runs in direction "
                + direction
                + "and I have no idea what a Stairlock is, but here it is "
                + stairLock);
        }
    }).start();

}

OOP Design

In General, be careful how you model objects.

If I understand you right, Card is-a Thread in your concept. That's awful! A Card can have-a thread, but it should never be one!

In good OOP design, a class has exactly one purpose, this is called Cohesion. Also, different kinds of functionality should be loosely coupled, i.e. Domain Classes should never contain infrastructure code.

Upvotes: 2

Manidip Sengupta
Manidip Sengupta

Reputation: 3611

Firstly, welcome to the world of learning Threads. Sure, you can extend Thread, just call the constructors with right signatures. Or, you could implement the Runnable interface, too. The two implements below (A and B) are equivalent in terms of functionality. Now, Mr. Floyd told us not to extend Thread. Lets explore why (I do not disagree, just trying to help you understand a design issue in Java).

Class extension is Java's implementation of inheritance, a basic OO paradigm. When you extend Thread directly, you are saying "My class (Person) 'is a' Thread", and that is perfectly acceptable. The reason most people do not extend Thread in this manner is Java's lack of multiple inheritance. Now don't quote me on that, I have no statistics to back it up and it is also possible that their IDE's do not allow it or supervisors do not like it. But here is the issue:

Without debating whether multiple inheritance is good or bad, we can see that it is not there and safely assume that it aint gonna be there in the near future. This means that if your class extends Thread, it cannot extend anything else. This puts you in a bind. However, java allows your class to implement multiple interfaces, and Runnable can be just one of them. This gives you more degrees of freedom as the object designer, as you can pick from (extend) a variety of well established classes to suit your design goals and still have the multithreading functionality.

Hope that helps. Regards, - M.S.

// A

public class Person extends Thread {

    public Person (int PersonID, Direction direction, StairLock stairLock) {
        super();
        // Other stuff for constructor
    }

    public void run () {
        // Whatever you want to do with the thread
    }

    public static void main (String[] args) {
        for (int i = 0 ; i < names.length ; i++) {
            Person ace = new Person (names[i], xxx, xxx);
            ace.start();
        }
    }
}

////////////////////////////////////////////////////////////

// B

public class Person implements Runnable {

    public Person (int PersonID, Direction direction, StairLock stairLock) {
        // Other stuff for constructor
    }

    public void run () {
        // Whatever you want to do with the thread
    }

    public static void main (String[] args) {
        for (int i = 0 ; i < names.length ; i++) {
            Person ace = new Person (names[i], xxx, xxx);
            new Thread (ace).start();
        }
    }
}

Upvotes: 1

Erick Robertson
Erick Robertson

Reputation: 33068

There are multiple issues with your question.

  • Any class which extends thread, in its constructor, can call super(String) to pass the thread name to the superclass thread. So in your constructor for Person, you can call this constructor for Thread immediately with the name you want for the thread. It is a good practice to always name your threads for debugging identification.
  • You refer to the class thread, but you should know this is different from Thread. Classes in Java should always start with uppercase letters. If you made your own thread class, you are not actually making a thread.
  • I'm not sure why a Card or a Person class should need to extend Thread. Unless you're just picking random names for this example, these classes sound more like data objects rather than threads. If this is the case, you might want to consider having a separate class be the Thread which handles the movement up and down the stairs, and give each thread its own Person class instance to handle.

Given all of this, if you were asked to make a thread for each Person to handle its movement, I would recommend creating a separate class from Person to do this. Either extend Thread or implement Runnable and take a Person in the constructor. Both classes use a .run() method that you have to implement to do the workload of the thread. With a Thread, you can just instantiate it and call .start(). With a Runnable, you have to create a new Thread and pass the Runnable as an argument to the constructor, then call .start() on the Thread.

Many people prefer to use the Runnable approach, but the results are pretty much the same either way. If you're handling threads, you should be familiar with both ways of doing it.

Upvotes: 6

Related Questions