Bent
Bent

Reputation: 1385

Java run multiple threads from for loop with different names

I am trying to start a variable number of threads within a for loop and want to name the threads prime1, prime2, [...].

The PrimeFinderThread Class extends from Thread.

[...]

    for (int i = 0; i <= numberThreads; i++) {
         PrimeFinderThread ("prime" + i) = new PrimeFinderThread (lowerBoundary, interval); 
    }

[...]

I am getting the error:

The left-hand side of an assignment must be a variable.

from ("prime" + i)

What is a possible soloution to start X Threads with a seperate name for each?

Upvotes: 0

Views: 2539

Answers (3)

Vikash Srivastava
Vikash Srivastava

Reputation: 36

You cannot specify additional parameters with X in left side assignment in Java. To give name to your threads you can call super(threadName) from the sub class of thread or thread.setName("threadName") can be used.

public class ThreadSubClass extends Thread{

    public ThreadSubClass(String threadName){
        super(threadName);
    }

    @Override
    public void run(){
        System.out.println("Entering : " + getName());
        //do Something
    }


    public static void main(String [] args){
        for(int i=0;i<5;i++){
            ThreadSubClass t = new ThreadSubClass("Prime"+i);
            t.start();
        }
    }

}

Upvotes: 0

Eran
Eran

Reputation: 393966

For an assignment in Java you cannot specify additional parameters on the left hand side of the = operator. So PrimeFinderThread ("prime" + i) is and will stay invalid.

Instead you should simply define an additional parameter for your constructor, and pass that to the parent class constructor using the super keyword construct.

You should assign the created threads to some variable

PrimeFinderThread[] threads = new PrimeFinderThread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
    threads[i] = new PrimeFinderThread ("prime" + (i+1), lowerBoundary, interval); 
    threads[i].start();
}

As for setting the name of each thread, that depends on what constructors your PrimeFinderThread class has. You can pass the thread name to the constructor of PrimeFinderThread, and from there, pass it to the constructor of Thread.

For example:

public PrimeFinderThread (String name, int lowerBoundary, int interval)
{
    super (name);
    ...
}

Upvotes: 2

wassgren
wassgren

Reputation: 19231

Try the following:

Thread[] threads = new PrimeFinderThread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
    threads[i] = new PrimeFinderThread (lowerBoundary, interval);
    threads[i].setName("prime" + i);
    threads[i].start();
}

It sets the name via the setName-method and then starts each thread via a call to start.

However, it is probably easier to make the constructor of PrimeFinderThread take an extra argument which is the name and invoke the super class constructor.

public PrimeFinderThread (String name, int lowerBoundary, int interval) {
    super(name); // instead of setName
    // Do the other stuff
}

IMO, an even better approach is to not extend the Thread class but rather provide a Runnable, preferably with some kind of ExecutorService - check e.g. the Executors JavaDoc or the concurrency trail from the Oracle website.

Upvotes: 3

Related Questions