Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Does a Cached Thread Pool Reuse Existing Threads

Tags:

I've just started looking at Java's Executors class and the newCachedThreadPool( ) method. According to the API, the resulting thread pool reuses existing Thread objects for new tasks.

I'm a bit puzzled how this is implemented because I couldn't find any method in the Thread API that lets you set the behaviour of an existing Thread object.

For example, you can create a new Thread from a Runnable object, which makes the Thread call the Runnable's run( ) method. However, there is no setter method in the Thread API that takes a Runnable as an argument.

I'd appreciate any pointers.

like image 594
Marc van Dongen Avatar asked Dec 04 '12 09:12

Marc van Dongen


1 Answers

Basically imagine each thread from the pool doing this:

public void run() {     while(true) {         if(tasks available) {            Runnable task = taskqueue.dequeue();            task.run();         } else {            // wait or whatever         }     } } 
like image 110
Tudor Avatar answered Nov 07 '22 04:11

Tudor