Java Multithreading Series--"Juc thread pool" 01 Thread pool Architecture
Overview
The "Java Multithreading Foundation", "Juc Atomic Class" and "Juc lock" are described in the previous sections. This chapter describes the last part of Juc, the thread pool. The content includes:
Thread Pool architecture diagram
Line Pooling Example
Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3509903.html
thread pool architecture diagram
The architecture diagram for the thread pool is as follows:
1. Executor
It is the "performer" interface, which is to perform the task. To be exact, executor provides an execute () interface to execute objects that have committed Runnable tasks. The purpose of executor exists is to provide a mechanism for separating "task submission" from "How Tasks work".
It contains only one function interface:
void execute (Runnable command)
2. Executorservice
Executorservice inherits from executor. It is the "performer Service" interface, which exists for the "performer Interface Executor" service, and Executorservice provides the "interface (Submit method) to deliver the task to the performer", "let the performer perform the task (InvokeAll, Invokeany method) "interface, and so on.
Executorservice List of functions
View Code
3. Abstractexecutorservice
Abstractexecutorservice is an abstract class that implements the Executorservice interface.
The purpose of Abstractexecutorservice exists is to provide a default implementation for the function interfaces in Executorservice.
List of Abstractexecutorservice functions
Since its list of functions is the same as Executorservice, it is not repeated here.
4. Threadpoolexecutor
Threadpoolexecutor is the famous "thread pool". It inherits from the Abstractexecutorservice abstract class.
List of Threadpoolexecutor functions
View Code
5. Scheduledexecutorservice
Scheduledexecutorservice is an interface that inherits from the Executorservice. It is equivalent to providing the executorservice of the "delay" and "cycle execution" functions.
Scheduledexecutorservice provides a corresponding function interface that can schedule tasks to be executed after a given delay, or to allow the execution of a task cycle.
List of Scheduledexecutorservice functions
View Code
6. Scheduledthreadpoolexecutor
The scheduledthreadpoolexecutor inherits from the Threadpoolexecutor and implements the Scheduledexecutorservice interface. It is equivalent to providing the scheduledexecutorservice of the "delay" and "cycle execution" functions.
Scheduledthreadpoolexecutor is similar to a timer, but in high concurrency programs, scheduledthreadpoolexecutor performance is better than a timer.
List of Scheduledthreadpoolexecutor functions
View Code
7. Executors
Executors is a static factory class. It returns objects of classes such as Executorservice, Scheduledexecutorservice, Threadfactory, and callable through a static factory method.
List of executors functions
View Code
Line Pooling Example
The following example shows a simple demonstration of the use of the thread pool.
1 Import java.util.concurrent.Executors; 2 Import Java.util.concurrent.ExecutorService; 3 4 public class ThreadPoolDemo1 {5 6 public static void Main (string[] args) {7 //Create a thread pool that can reuse a fixed number of threads 8
executorservice pool = Executors.newfixedthreadpool (2); 9 //Create implements the Runnable interface object, the thread object of course also implements the Runnable interface of ten thread ta = new MyThread (); one thread TB = new MyThread (); the thread TC = new MyThread (), the thread td = New MyThread (), and the thread te = new MyThread (); Line implementation of Pool.execute (TA), pool.execute (TB), Pool.execute (TC), Pool.execute (TD); 20 Pool.execute (TE); //Close thread pool pool.shutdown (); }24}25-class MyThread extends thread {27 28< c20/> @Override29 public void Run () { System.out.println (Thread.CurrentThread (). GetName () + "is running."); }32}
Operation result :
Pool-1-thread-1 is Running.pool-1-thread-2 is running.pool-1-thread-1 are Running.pool-1-thread-2 is Running.pool-1-thread-1 is running.
Result Description :
The thread pool pool is created in the main thread, and the capacity of the thread pool is 2. That is, you can run up to 2 threads in a thread pool at the same time.
Immediately thereafter, the Ta,tb,tc,td,te 3 threads are added to the thread pool to run.
Finally, the thread pool is closed by shutdown ().
Java-"Juc thread pool" schema