Reuse Java Stream

I always read that creating threads is expensive. I also know that you cannot repeat the stream.

I see the Executors class in the document: Creates a pool of threads, which if necessary creates new threads, but will use the previously created threads when they are available.

Remember the word "reuse".

How do thread threads reuse threads?

+46
java multithreading
Feb 24 '10 at 6:06
source share
5 answers

I think I realized what scares you, so here is my longer answer: terminology is a tiny bit misleading (obviously, or you won’t ask this question, specifically putting emphasis on β€œreuse”):

How do thread threads reuse threads?

What happens is that a single thread can be used to handle several tasks (usually passed as Runnable , but it depends on your "executor" structure: executors accept Runnable by default, but you can write your own "executor" / thread - pool accepts something more complex than Runnable [for example, a CancellableRunnable ]).

Now in the default implementation of ExecutorService , if a thread somehow terminates during use, it is automatically replaced by the new thread, but this is not the "reuse" they are talking about. In this case, "reuse" is absent.

So, the truth is, you cannot call start() twice in a Java thread, but you can pass as many Runnable as you want to the executor, and each Runnable run() method must be called once.

You can pass 30 Runnable up to 5 Java Thread , and each worker thread can call, for example, run() 6 times (practically does not guarantee that you will execute exactly 6 Runnable per Thread but this is a detail).

In this example, start() could be called 6 times. Each of these 6 start() will call exactly once the run() method for each Thread :

From Thread.start() Javadoc:

  * Causes this thread to begin execution; the Java Virtual Machine * calls the <code>run</code> method of this thread. 

BUT , then inside each run() thread, the Runnable method must be removed, and the run() method for each Runnable will be called. Therefore, each thread can handle several Runnable . This is what they call "stream reuse".

One way to make your own thread pool is to use the blocking queue that you run runnables on and have each of your threads after it has processed the run() Runnable method, delete the next Runnable (or block) and run its run() , then rinse and repeat.

I assume that part of the confusion (and this is a bit confusing) is because a Thread accepts Runnable and when calling start() the Runnable run() method is called, while the default thread pools also take Runnable .

+42
Feb 24 '10 at 6:29
source share

The thread method run threads in a thread pool consists of more than just one task. The run method of a thread in a thread pool contains a loop. He pulls the task out of the queue, performs the task (which returns to the loop when it is completed), and then receives the next task. The run method does not end until the thread is no longer needed.

Edited to add:

Here is the run method of the inner Worker class in ThreadPoolExecutor .

 696: /** 697: * Main run loop 698: */ 699: public void run() { 700: try { 701: Runnable task = firstTask; 702: firstTask = null; 703: while (task != null || (task = getTask()) != null) { 704: runTask(task); 705: task = null; // unnecessary but can help GC 706: } 707: } finally { 708: workerDone(this); 709: } 710: } 
+12
Feb 24 '10 at 6:28
source share

A thread pool consists of a series of fixed workflows that can perform tasks from an internal task queue. Therefore, if one task ends, the thread does not end, but waits for the next task. If you interrupt the stream, it will be automatically replaced.

See more details.

+4
Feb 24 '10 at 6:12
source share

The thread pool creates its own threads and supplies its own smart little Runnables for these threads. These Runnables never end, but are synchronized in the queue (they wait ()) until Callable appears in this queue; they are notified when this happens, and their Runnable starts Callable from the queue, and the whole script repeats again.

0
Mar 18 '17 at 16:43
source share

If thread finished, be sure to repeat it again. You can make sure that it does not work by calling isAlive() or something like that.

EDIT . If someone paused thread , you cannot start again. I do not understand why it cannot be started if it finished normally. But I give benefit to doubts and say that it cannot be reused.

-four
Feb 24 '10 at 6:13
source share



All Articles