Creating dynamic number of threads concurrently

38,440

Solution 1

But, I don't understand how to start these n number of threads behaving like multi threading concept. I want them to run in parallel.

You can certainly create an array of threads using a loop:

 Thread[] threads = new Thread[NUM_JOBS_TO_CREATE];
 for (int i = 0; i < threads.length; i++) {
     threads[i] = new Thread(new Runnable() {
         public void run() {
             // some code to run in parallel
             // this could also be another class that implements Runnable
         }
     });
     threads[i].start();
 }

This will cause the threads to run in the background in parallel. You can then join with them later to wait for them all to complete before continuing.

// wait for the threads running in the background to finish
for (Thread thread : threads) {
    thread.join();
}

But instead of managing the threads yourself, I would recommend using the builtin Java Executors. They do all of this for you are are easier to manage. One of the benefits of this method is that it separates the tasks from the threads that run them. You can start, for example, 10 threads to run 1000s and 1000s of tasks in parallel.

Here's some sample ExecutorService code:

 // create a pool of threads, 10 max jobs will execute in parallel
 ExecutorService threadPool = Executors.newFixedThreadPool(10);
 // submit jobs to be executing by the pool
 for (int i = 0; i < NUM_JOBS_TO_CREATE; i++) {
    threadPool.submit(new Runnable() {
         public void run() {
             // some code to run in parallel
             // this could also be another class that implements Runnable
         }
     });
 }
 // once you've submitted your last job to the service it should be shut down
 threadPool.shutdown();
 // wait for the threads to finish if necessary
 threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);

For more info, see the Java tutorial on the thread executors.

Solution 2

Try very hard to not create arrays of threads and attempt to manage them - it will soon turn in to an apalling mess. If you need a pool of threads to run tasks, you need a producer-consumer queue. Create one and pass it, (or the threadpool object instance that contains it as a member), into the threads as you create them. The threads loop round, fetching tasks and executing them.

The easy way to do this is to use an ExecutorService as detailed by @Gray, (+1).

For emphasis, I say again, don't try to micro-manage threads in arrays, lists or vectors, starting them, checking their state in a 'boss/management' loop, terminating/aborting them, destroying them etc. etc. It's like a Porsche 911 - after the expenditure of a huge amount of money/time to get one, you will have something that seems to work OK and then it will suddenly break and spin you into a tree.

Use a dedicated thread for jobs that block for extended periods, a threadpool for those that can be done intensively and quickly.

Solution 3

Basic psuedocode:

create x threads, store them in an array or list;
for each thread in the list/array
   call start() on the thread object;

Solution 4

In a class that needs to be multithreaded I set at the top of the class:

private static final ExecutorService executor = Executors.newCachedThreadPool();

& in Java 8+ use a lamda expression wherever I need something to run in a new thread:

executor.submit(() -> {
        myOtherClass.myFunction(doSomething);
});

With a newCachedThreadPool Java will manage the total number of threads according to the number of cpu cores on the system & automatically stop them after a period of inactivity (60 seconds by default).

Share:
38,440
sowmya
Author by

sowmya

Updated on March 10, 2020

Comments

  • sowmya
    sowmya about 4 years

    Each time I have to create a variable number of threads. I do this by creating an array of Threads and create multiple number of threads.

    But, I don't understand how to start these n number of threads behaving like multi threading concept. I want them to run in parallel.

    Please guide if what to do in this senario.

  • sowmya
    sowmya about 12 years
    If I do so, the threads are not running in parallel. But, they are running sequentially as a normal function call
  • Hunter McMillen
    Hunter McMillen about 12 years
    @sowmya I think you need to read up on threads. Calling start() on a bunch of threads sequentially does not mean that they run sequentially, it just means they are started sequentially.
  • sowmya
    sowmya about 12 years
    But, they are executing the function sequentially in the order in which I start them. As per my knowledge, at least the output should change. But, it is not happening. Correct me if I am wrong.
  • Hunter McMillen
    Hunter McMillen about 12 years
    They each execute their own version of the function.
  • Manoj Ramanan
    Manoj Ramanan over 6 years
    if I have 1000 jobs it will create 1000 objects would that result in a performance problem
  • Gray
    Gray over 6 years
    1000 objects are pretty cheap @ManojRamanan. 1000 strings are more expensive from an object bandwidth perspective. You might notice some GC issues if you created a million objects so we typically put boundaries on the queue in case the Runnable producer is much faster than the consumers.
  • Manoj Ramanan
    Manoj Ramanan over 6 years
    Thanks, @Gray for the information.can you pls share some more information i am currently using this implementation.how do you test it
  • Theo
    Theo about 5 years
    Welcome to Stack Overflow. While this may very well be code that solves the OP's problem, please add some explanation to what the code does and why this answers the question.