Reference: http://www.jianshu.com/p/87bff5cc8d8c
Http://www.jianshu.com/p/edd7cb4eafa0
In Java applications, if you need to frequently create a large number of threads to complete the task, it will certainly have a significant impact on performance. Because the system consumes too much resources to create and destroy threads, it is expensive.
So in this case the thread pool can be considered. Has the following advantages:
1) Reduce resource consumption: Reduce the consumption caused by thread creation and destruction by reusing the threads that have been created;
2) Improve response speed: When the task arrives, the task can be executed without waiting for the thread to be created;
3) Improve thread manageability: threads are scarce resources, and if created indefinitely, not only consumes system resources, but also reduces system stability, using a thread pool for uniform allocation, tuning and monitoring
The executor framework introduced in Java1.5 defines four ways to create a thread pool:
First, Newfixedthreadpool
Private Static Executor Executor = Executors.newfixedthreadpool (ten);
The fixed thread pool, similar to Newcachethreadpool (), is also capable of resue (multiplexing), but this pool specifies the maximum number of threads, meaning that when the pool is idle, the new task will be executed in the idle thread, and once the thread Cheng threads are working, Then the new task has to wait for the thread pool to be free to enter the thread pool, and the other tasks continue to wait in line. Such pools do not stipulate how long their free time is. This type of pool is more suitable for server
Second, Newcachedthreadpool
Private Static Executor Executor1 = Executors.newcachedthreadpool ();
Cache the thread pool to see if there is a cache of the current execution thread in the resue, if so, if not, then create a thread to complete the current call. And this kind of thread pool can only do some short-lived tasks. And this kind of thread pool internal rules can resue (multiplexing) Thread, the idle time can not exceed 60s, once exceeded 60s, will be moved the thread pool
Third, Newsinglethreadexecutor
Private Static Executor Executor2 = Executors.newsinglethreadexecutor ();
There is only one thread in the initialized thread pool, and if the thread ends abnormally, a new thread is recreated to continue the task, and the only thread can guarantee the order in which the submitted tasks are executed, using Linkedblockingqueue as the blocking queue internally.
Iv. Newscheduledthreadpool
Private Static Executor Executor3 = Executors.newscheduledthreadpool (ten);
The scheduling thread pool, the scheduled thread pool, is deferred based on the scheduled (Task list) or is periodically executed. Suitable for some cyclical work.
After the task has been created, a commit run is required, and two submission methods are available in the online pool framework:
First, Executor.execute ()
Arguments submitted through the Execute method must implement the Runnable interface, which is executed in the Run method, and the task submitted by the method does not have a return value.
// no return value Static class Task implements runnable{ @Override publicvoid run () { System. out . Print (Thread.CurrentThread (). GetName ()); } }
Second, Executorservice.submit ()
The parameters submitted through the Submit method need to implement the callable interface, the task is executed in the call method, the method has a return value, you can use the return value to determine the execution status of the task.
// has a return value Static class Calltask implements Callable<string>{ @Override public String Call () { return (Thread.CurrentThread (). GetName ()); } }
Introduction to the thread pool in Java