Starting 100 threads in parallel and starting missing threads if some previous ones are completed

For example, I need to always start 100 threads to perform some actions. I have a class called ThreadsWorker that searches for the number of threads and starts the missing threads if some of the previous ones are completed. So this is a table describing the situation:

 1 second: 100 threads 2 second: 92 threads (ThreadsWorker generates new 8 threads) 3 second: 100 theads 4 second: 72 threads (ThreadsWorker generates 28 threads) 

And so on. My threads are anonymous calls (just new Thread(new Runnable(...)).start() ) because I don’t know how to save them in the Threads[] array correctly, because while ThreadsWorker will save threads[i] = new Threads() , some threads can be finished, and then there will be some collision with the array indices.

Due to anonymous calls, I now use the threadsCount variable and increase it at the beginning of the body and decrease its length (using synchronized ). Well, it works correctly, and the only way is to use a while() , which checks if threadsCount == 0 when progress is complete.

I think this is C-style, but not the Java way :) So, can you help me do this in the Java way?

+4
source share
5 answers

If your goal is to actively process 100 threads, I suggest looking at the thread pools (and Executors in general).

It is unclear whether you want to save all 100 threads or wait for them to complete. Your question refers to both (ThreadsWorker creating 28 new threads, threadCount == 0), and they seem contradictory.

+6
source

Put all threads in an array or collection.

Then iterate over the collection that calls Thread.join () for each. When this loop ends, all threads are executed.

 ArrayList threads = new ArrayList(); for (int i = 0; i < 5; i++) { Thread t = new AweseomeThread(); t.start(); threads.add(t); } for (Thread t : threads) { t.join(); } 

You will also need exception handling (e.g. InterruptedException). But I will leave this as an exercise for the reader ... :)

+5
source

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html

You can try the class CountDownLatch jdk api

 private CountDownLatch latch; private static class SimpleThread extends Thread { public void run() { latch.countDown(); } } public static void main(String[] args) { int threadcount = 10; latch = new CountDownLatch(threadcount); for (int i = 0; i < 10; i++) { Thread t = new SimpleThread(); t.start(); } // waiting threads all finished latch.await(); } 

Extract the number of threads from the latch attribute of the main class

+3
source

I believe that you are trying to introduce ThreadWorker to new threads for all completed threads.

I would use BlockingQueue so that the threads (your Runnable (s)) are added at the end. ThreadWorker will wait for the thread to complete and then start a new thread.

 public class YourRunnable implements Runnable { private final BlockingQueue<YourRunnable> queue; public YourRunnable(BlockingQueue<YourRunnable> queue){ this.queue = queue; } public void run{ // Your Code... // Finished Processing queue.add(this); } } public class ThreadWorkder implements Runnable { private final BlockingQueue<YourRunnable> queue; ThreadWorker(BlockingQueue<YourRunnable> queue){ this.queue = queue; } public void run{ while(queue.take()){ (new Thread(new YourRunnable(queue))).start(); } } // general main method public static void main(String [] args){ BlockingQueue<YourRunnable> queue = new LinkedBlockingQueue<YourRunnable>(); ThreadWorker worker = new ThreadWorker(queue); Thread(worker).start(); for (int i = 0; i < 100; i++){ (new Thread(new YourRunnable(queue))).start(); } } } 
+2
source

Use a collection instead of an array. Upon completion of the threads, remove them from the array. Something like that:

 public class Foo { Vector<Thread> threads = new Vector<Thread>(); //Vector is threadsafe public ensureThreadCount(int count) { while (threads.size() < count) { Thread t = new AweseomeThread(threads); threads.add(t); t.start(); } } } public class AwesomeThread { Collection threads; public AwesomeThread(Collection threads) { this.threads = threads; } public void run() { try { // do stuff } catch (Throwable t) { } finally { threads.remove(this); } } } 

Then ask your worker to call Foo.ensureThreadCount ().

+1
source

Source: https://habr.com/ru/post/1346159/


All Articles