What happens when a thread is not free in the thread pool and we send the task to the pool?

Will the thread creation method wait for the thread to free? can i reduce the number of threads generated by the thread pool?

+4
source share
3 answers

If you use a pool of cached threads, the pool will create more threads. However, this will only be the maximum necessary at any given time, and may be much less than the number of tasks you submit.

If you use a fixed-size thread pool, it will create a fixed number of threads, regardless of whether you are setting any tasks or setting more tasks for you than it can do. He will stand in line for any expected tasks.

Will the thread creation method wait for the thread to free?

While you can create a queue that does this, this is not the default behavior. A more common solution is to force the caller to complete the task, if necessary.

Is it possible to reduce the number of threads generated by a thread pool?

A thread pool can cause far fewer threads than esp tasks if you limit the number of threads.

+2
source

Will the thread creation method wait for the thread to free?

. , , . , , . ( ) .

, ?

, .. , , () , , #tasks > max threads in pool , .

+1

ThreadPoolExecutor:

ThreadPoolExecutor (. getPoolSize()) , corePoolSize (. getCorePoolSize()) maximumPoolSize (. getMaximumPoolSize()). execute (java.lang.Runnable), , corePoolSize, , . , corePoolSize, , maximumPoolSize , , . corePoolSize maximumPoolSize , . maxPoolSize , Integer.MAX_VALUE, . , , setCorePoolSize (int) setMaximumPoolSize ().

, : "core" "max". , , "", . , "" , , . , "max". , , .

In general, there is no “right” way for thread pools to work. Any specific implementation can be used: a pool of threads of a fixed size, which always has threads X, or a pool of threads, which always grows to the maximum limit, etc.

+1
source

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


All Articles