Java multithreading Summary
Preface
Life's a climb, but the view is great.
1. Three ways to create a thread
1.1 inherit from Thread
1.2 Runnable1.3 Callable
2. thread status2.1 Five statuses: creation, ready, running, blocking, and death 2.2
Terminating thread: natural termination, external interference2.3 blocking:
Join and yield, Sleep
Difference between sleep method and wait method:The two subordinate classes are different.
Sleep: sleep does not release the lock from the Thread class.
Wait: the lock to be released belongs to the Object class.
3. Thread Information
Thread. currentThread ()Current thread, get name, Set Name, set priority, determine status
4. Thread Synchronization(For the same resource)
Why use synchronization?
The access to the same resource by multiple threads is insecure. To ensure resource accuracy and security, we need to add synchronization.
4.1 Synchronization Method
Modifier synchronized Method Name ()
{
Method body
}
4.1 synchronization Block
Synchronized (reference type | this | class. class ){
}
Note:Too many syncs may cause deadlocks.
V. producer and consumer model
Vi. Task Scheduling
Timer, ScheduledExecutor, etc.