Create a thread pool that can hold 3 threads executorservice fixedthreadpool = Executors.newfixedthreadpool (3); public static Executorservice newfixedthreadpool (int nthreads) { return new Threadpoolexecutor (Nthreads,//core Pool Size nthreads,//maximum pool size 0L, //keep alive Time timeunit.milliseconds, new Linkedblockingqueue<runnable> ()); }
Cachedthreadpool creates a buffer that caches the initialized thread. Terminates and removes a thread that has not been used for 60 seconds from the cache.
If the thread is available, use the previously created thread,
If the thread is not available, the new thread is created.
The task is executed alternately
- reuse: cache pool, first see if there are no previously established threads, if any, reuse; if not, build a new thread to join the pool
- usage Scenario: A cache pool is often used to perform asynchronous tasks that are short-lived, so it is not used in some connection-oriented daemon servers.
- timeout: The thread that can reuse, must be timeout idle in the pool threads, the default timeout is 60s, more than this idle time, the thread instance will be terminated and removed from the pool.
- End: Note that the thread that is put into the cachedthreadpool does not have to worry about its end, exceeding timeout inactivity, which is automatically terminated.
The size of the thread pool is dynamically allocated according to the number of tasks performed executorservice Cachedthreadpool = Executors.newcachedthreadpool (); public static Executorservice Newcachedthreadpool () { return new Threadpoolexecutor (0, //core pool size Integer.max_value,//maximum pool size 60L, //keep alive Time timeunit.seconds, new Synchronousqueue<runnable> ()); }
Creates a thread pool of a single threads, and if the current thread breaks abruptly when the task is executed, a new thread is created to replace it to continue with the task executorservice singlethreadpool = Executors.newsinglethreadexecutor (); public static Executorservice Newsinglethreadexecutor () { return new Finalizabledelegatedexecutorservice ( New Threadpoolexecutor (1, //core pool size 1, //maximum pool size 0L,//keep alive Time Timeunit.milliseconds, new Linkedblockingqueue<runnable> ())); }
The effect is similar to the timer timer scheduledexecutorservice scheduledthreadpool = Executors.newscheduledthreadpool (3); Public scheduledthreadpoolexecutor (int corepoolsize) { super (corepoolsize, //core pool size Integer.max _value,//maximum Pool size 0, //keep alive Time timeunit.nanoseconds, new Delayedworkqueue ()); }
Data sources
http://blog.csdn.net/vking_wang/article/details/9619137
Fixedthreadpool
In Fixedthreadpool, there is a fixed-size pool.
If the task you currently need to perform exceeds the pool size, then the extra task is waiting until a thread with idle threads executes the task.
If the task currently needs to be performed is less than the pool size, the idle thread is not destroyed.
- reuse:Fixedthreadpool and Cachethreadpool are similar, but also can reuse use, but can not build new threads at any time
- Fixed number: What is unique is that at any point in time, there can be at most a fixed number of active threads, and if a new thread is to be established, it can only wait in another queue until a thread in the current thread terminates and is moved directly out of the pool.
- timeout: Unlike Cachethreadpool, Fixedthreadpool does not have an idle mechanism (possibly, but since the document is not mentioned, it must be very long, similar to the TCP or UDP idle mechanism that relies on the upper layer),
- usage Scenario: so Fixedthreadpool most for some very stable and fixed regular concurrent threads, more for the server
- Source Analysis: from the source code of the method, the cache pool and the fixed pool call the same underlying pool, except that the parameters are different:
Fixed pool thread count, and is 0 seconds idle (no idle)
Cache Pool Threads Support 0-integer.max_value (obviously not considering the host's resource tolerance), 60 seconds idle
Java thread pool Executorservice