Base
-
Process: Each process has its own code and data space (process context). Switching between processes can be expensive. A process contains 1 to N threads. (Processes are the smallest unit of resource allocation.)
-
Thread: The same type of thread sharing code and data space, each thread has an independent run stack and program counter (PC), thread switching overhead is small. (Threads are the smallest unit of CPU scheduling.)
Implementing multithreading
- Thread class inheritance
- Implement the Runnable interface
Implementing the Runnable interface has advantages over inheriting the Thread class:
-
You can avoid the restriction of single inheritance in Java
-
Thread pools can only be placed into classes that implement Runable or Callable, not directly into classes that inherit threads
Thread pools perform tasks
Callable
Future
FutureTask
Thread state
Create, ready, run, block, terminate.
New | Runnable | Running | Blocked | Dead |
---|
Thread safety
The thread pool
Resource control, thread management, avoid the overhead problem caused by repeated creation and destruction of threads
- Repeatedly creating and destroying threads is expensive
- Too many threads take up too much memory
parameter
corePoolSize
Core threads
Rule for adding threads:
-
If the number of threads is less than corePoolSize, create a core thread
-
If the number of threads is equal to or greater than corePoolSize but less than maxPoolSize, the task is queued
-
If the queue is full and the number of threads is full, a new non-core thread is created to execute the task
-
If the queue is full and the number of threads is greater than or equal to maxPoolSize, the task is rejected
CorePoolSize > workQueue > maxPoolSize
maxPoolSize
Maximum number of threads
keepAliveTime
Threads that exceed corePoolSize and exceed keepAlveTime are terminated
The alive time of a non-core thread
ThreadFactory
Default Executors. DefaultThreadFactory () to create
You can also customize your ThreadFactory, which allows you to change the thread name, thread group, priority, and whether or not it is a daemon
workQueue
SynchronousQueue SynchronousQueue
Unbounded queue: LinkedBlockingQueue
Bounded queue: ArrayBlockingQueue
WorkStealingQueue (since 1.8)
Suitable for tree traversal, there are many sub-tasks to perform
Each thread’s subtasks are placed in its own queue, and other threads can steal the tasks in the queue to help execute them
Handler
Refused to timing
-
When Executor is down, submitted new tasks are rejected
-
When using bounded queues, new submitted tasks are rejected
RejectPolicy:
-
AbortPolicy — Default
An exception is thrown
-
DiscardPolicy
Directly discarded
-
DiscardOldestPolicy
Discards the oldest queue
-
CallerRunsPolicy
Let the caller run the task
advantages
-
Business loss is avoided
-
Reducing the speed of the submission is a negative feedback that the submission will continue only after the task is completed, during which time some of the tasks in the queue are also executed, which is equivalent to giving the thread pool some buffer time
-
Create thread pools manually – determine thread pools in conjunction with your business
Determining the number of threads
Thread pools provided by the JDK
newFixedThreadPool
newSingleThreadPool
newCachedThreadPool
newScheduleThreadPool
Stopping the thread pool
Hook method
Before and after each task is executed, log, count custom thread pools, add pause and resume functions