One, Java thread pool
Java provides four thread pools through executors, namely:
1, Newcachedthreadpool: Create a cacheable thread pool, if the thread pool length exceeds the processing needs, can flexibly reclaim the idle thread, if not recyclable, the new thread. (Maximum number of threads is not controllable)
2. Newfixedthreadpool: Create a thread pool to control the maximum number of concurrent threads, and the excess threads will wait in the queue.
3, Newscheduledthreadpool: Create a fixed-line pool, support timing and periodic task execution.
4. Newsinglethreadexecutor: Create a single threaded thread pool that performs tasks with only a single worker thread, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority).
The advantage of a thread pool comparison single thread is:
A. Reusing existing threads, reducing the cost of object creation, extinction, and performance.
B. Can effectively control the maximum number of concurrent threads, improve the utilization of system resources, while avoiding excessive resource competition, avoid clogging.
C. Provide functions such as timed execution, periodic execution, single-threaded, concurrency control, etc.
Reference: http://blog.csdn.net/nk_tf/article/details/51959276
Executor, the difference between Executorservice and executors
From: Https://www.baidu.com/link?url=8K_op4j3ORuqN8dDsh4QEesx5LUE6fKTPSoor1l1ce_ 0dndrqkyyfr7y4fti5txvwfvidptzr0s3ckeaictcka&wd=&eqid=ebd4c02600016502000000045bc5544b
Here is a detailed look at the difference between the three:
Executor vs Executorservice vs Executors
As mentioned above, all three are part of the Executor framework. Java developers have a great need to learn and understand them in order to use the different types of thread pools provided by Java more efficiently. Summarize the differences between the three, so that we can better understand:
Executor and Executorservice: The difference 1:executorservice interface inherits the Executor interface
Executor and Executorservice: The Execute () method that distinguishes the 2:executor interface is used to receive the object of the Runnable interface.
The Executorservice interface's submit () method can receive the Runnable interface and the callable interface object.
Executor and Executorservice: The Execute () method that distinguishes 3:executor returns void,
The submit () method of Executorservice can be returned by a future object (the result of the operation).
Executor and Executorservice: Difference 3: In addition to allowing a client to submit a task, Executorservice also provides a way to control the thread pool. For example: Call the ShutDown () method to terminate the thread pool. You can learn more about shutting down the thread pool and how to handle pending tasks through the Java Concurrency in practice.
The Executors class provides a factory method to create different types of thread pools. For example: Newsinglethreadexecutor (): Create a thread pool with only one threads,
Newfixedthreadpool (int numofthreads): Creates a thread pool with a fixed number of threads,
Newcachedthreadpool (): Creates a new thread as needed, but reuses a wired thread if it is idle.
Executor |
Executorservice |
Executor is the core interface of the Java thread pool, used to execute the submitted task concurrently |
Executorservice is an extension of the Executor interface, providing a way to execute and close the thread pool asynchronously |
Provides the Execute () method to submit a task |
Provide a submit () method to submit a task |
Execute () method has no return value |
The Submit () method returns the future object, which can be used to get the result of the task execution |
Cannot cancel task |
Tasks in pending can be canceled by Future.cancel () |
There are no methods for providing and shutting down the thread pool |
Provides a way to turn off the thread pool |
Executor, the difference between Executorservice and executors