Arthur
Arthur

Reputation: 151

Creating and naming multiple, simultaneous threads with a for loop

Is there a way to create multiple threads that run simultaneously with a for loop? Consider this example:

for(int i = 1; i<=36; i++) {
     if(new Random().nextInt(2)==0){
         ActionThread nr = new ActionThread();
     }
}

I don't want the threads to be killed after completion of the if statement. The end of each thread is randomly determined in the ActionThread class itself. Also, how do I name the threads automatically? For example, instead of nr, the first thread should be named nr1, the second nr2, the third nr3, and so on.

Upvotes: 1

Views: 92

Answers (3)

Stephen C
Stephen C

Reputation: 719299

I'm assuming that ActionThread is some custom class that you have created that extends Thread.

I don't want the threads to be killed after completion of the if statement.

They won't be. However, it doesn't look like you have started them yet. Read the javadocs for Thread. Read the material at the top, then look at the start() and run() methods.

If you don't start a thread ... nothing happens.

Also, if you want some other part of your application to be able to "do things" to the threads once they have been created, you should replace the nr local variable with a data structure that the the rest of the application can get at; e.g. a list or an array.

(It is also possible to find extant threads via the ThreadGroup tree, but it is complicated.)

Also, how do I name the threads automatically?

Call Thread.setName(), or pass the thread name to the (relevant) Thread constructor. For example:

  nr.setName("thr" + i);

Or you could even make your ActionThread set its own name in the constructor.


I should also point out that is is generally considered to be a bad idea to create subclasses of Thread. It is better to put your thread logic into a custom Runnable class, then create and pass a Runnable instance as a Thread construct argument. Like this:

    public class MyRunnable implements Runnable {
         @Override
         public void run() {
              // thread logic goes here
         }
    }

    Thread th = new Thread(new MyRunnable());
    th.start();

If you want to pass parameters to the thread logic, add a constructor to your runnable class with some arguments, and provide them when you instantiate the runnable.

Why do it this way? Because it allows you to easily change your code to use a thread loop or executor or some such.

Upvotes: 1

Nitin Dandriyal
Nitin Dandriyal

Reputation: 1607

public static void main(String[] a) {
    List<ActionThread> threads = new ArrayList<>();
    for (int i = 1; i <= 36; i++) {
        if (new Random().nextInt(2) == 0) { // no idea why you have put this
                                            // but seems unecessary
            ActionThread thread = new ActionThread();
            threads.add(thread);
            thread.start();
        }
    }
}
class ActionThread extends Thread {
    @Override
    public void run() {
        // Write what to do in Thread here
    }
}

Once the list of ActionThread is there you have handle to all the Threads that you have created. using threads.get(index). From question its appears that by name you meant handle to Thread instance

Upvotes: 1

mr mcwolf
mr mcwolf

Reputation: 2859

For automatic naming, may be use static field (counter) in ActionThread and increment him in the constructor, before generate thread name.

 class ActionThread extend Thread {
      private static int id = 0;
      ActionThread() {
           setName(String.format("n%d", ++id);
      }
 }

Upvotes: 0

Related Questions