Java underlying technology series-thread pool framework, java underlying
I. Thread Pool Structure
Ii. Example
Define thread Interface
Public class MyThread extends Thread {
@ Override
Publicvoid run (){
System. out. println (Thread. currentThread (). getName () + "executing ");
}
}
1: newSingleThreadExecutor
ExecutorService pool = Executors. newSingleThreadExecutor ();
Thread t1 = new MyThread ();
Thread t2 = new MyThread ();
Thread t3 = new MyThread ();
// Put the thread into the pool for execution
Pool.exe cute (t1 );
Pool.exe cute (t2 );
Pool.exe cute (t3 );
// Close the thread pool
Pool. shutdown ();
Input result:
Pool-1-thread-1 is being executed
Pool-1-thread-1 is being executed
Pool-1-thread-1 is being executed
2: newFixedThreadPool
ExecutorService pool = Executors. newFixedThreadPool (3 );
Thread t1 = new MyThread ();
Thread t2 = new MyThread ();
Thread t3 = new MyThread ();
Thread t4 = new MyThread ();
Thread t5 = new MyThread ();
// Put the thread into the pool for execution
Pool.exe cute (t1 );
Pool.exe cute (t2 );
Pool.exe cute (t3 );
Pool.exe cute (t4 );
Pool.exe cute (t5 );
Pool. shutdown ();
Input result:
Pool-1-thread-1 is being executed
Pool-1-thread-2 is being executed
Pool-1-thread-1 is being executed
Pool-1-thread-2 is being executed
3: newCachedThreadPool
ExecutorService pool = Executors. newCachedThreadPool ();
Thread t1 = new MyThread ();
Thread t2 = new MyThread ();
Thread t3 = new MyThread ();
Thread t4 = new MyThread ();
Thread t5 = new MyThread ();
// Put the thread into the pool for execution
Pool.exe cute (t1 );
Pool.exe cute (t2 );
Pool.exe cute (t3 );
Pool.exe cute (t4 );
Pool.exe cute (t5 );
// Close the thread pool
Pool. shutdown ();
Input result:
Pool-1-thread-2 is being executed
Pool-1-thread-4 is being executed
Pool-1-thread-3 is being executed
Pool-1-thread-1 is being executed
Pool-1-thread-5 is being executed
4: ScheduledThreadPoolExecutor
ScheduledExecutorService pool = Executors. newScheduledThreadPool (2 );
Pool. scheduleAtFixedRate (new Runnable () {// an exception is triggered every time
@ Override
Public void run (){
// Throw new RuntimeException ();
System. out. println ("================== ");
}
}, 1000,200 0, TimeUnit. MILLISECONDS );
Pool. scheduleAtFixedRate (new Runnable () {// print the system time at intervals, proving that the two do not affect each other
@ Override
Public void run (){
System. out. println ("++ ");
}
}, 1000,200 0, TimeUnit. MILLISECONDS );
Input result:
======================
++
++
++
Iii. Core Thread Pool Parameters
CorePoolSize: number of core threads in the pool
MaximumPoolSize: Maximum number of threads allowed in the pool.
KeepAliveTime: when the number of threads exceeds the core, this is the maximum time for Idle threads to wait for new tasks before termination.
Unit: The time unit of the keepAliveTime parameter.
WorkQueue: the queue used to keep tasks before execution. This queue only keeps Runnable tasks submitted by the execute method.
ThreadFactory: The factory used by the execution program to create a new thread.
Handler: The processing program used to block execution because it exceeds the thread range and queue capacity.
ThreadPoolExecutor: underlying implementation of the Executors class.
3.1 task queuing mechanism
SynchonousQueue: Synchronous queue. The queue is directly submitted to the thread for execution without holding them. In this case, the thread pool is generally unbounded.
Define blockingqueue: unbounded columns. When the number of threads in the thread pool reaches the maximum, new tasks will be waiting for execution in the queue, which may cause infinite expansion of the queue.
ArrayBlockingQueue: A Bounded queue helps prevent resource depletion. Once the upper limit is reached, new tasks may be lost.
Note:
NewSingleThreadExecutor and newFixedThreadPool use javasblockingqueue.
NewCachedThreadPool uses SynchonousQueue.
NewScheduledThreadPool uses DelayedWorkQueue
3.2 thread Execution Process
3.3 determine the thread size:
Cpu-intensive: use as few threads as possible. The optimum number of threads is Ncpu + 1
Io-intensive: multiple threads, 2 Ncpu
Hybrid mode: Depending on the situation, it can be split into io-intensive and cou-intensive