1. Thread Status
For example, when we create a new thread and start, it does not necessarily happen immediately, because only the operating system dispatches our thread to actually execute, and the operating system can run other threads at any time, and the thread goes back to the operational state. This process is controlled by the operating system, not what we can control. What we can control is to turn the thread into blocked and change from blocked to runable state.
In the previously experimental wait and notify, I did not notice the notify after the lock phase, thought notify directly into the runable state, waiting for the operating system scheduling. From the above figure can be seen, we notify, the actual scene into the locked state, a thread directly into the runable state, but in the case of multithreading, because we wait or notify is in sync, Only the thread that obtains the sync lock has the opportunity to enter Runable, and the other threads have to wait.
So here's the main way to learn about the lock principle.
2. Lock
1). Built-in lock
Every object in Java can be used as a synchronous lock, which is a built-in lock, or a lock is monitored, and a built-in lock is obtained by means of a synchronous method or block of code, that is, using the Synchronized keyword
2). Display lock
The advanced locks provided by the JDK, such as lock, condition, etc., are mainly used for the functions of the built-in lock not easy to implement
3. Some features of the lock
1). Can be re-entered
Java Threads Compute locks on a per-thread basis, and in the same thread, you can enter the synchronization method again without causing a deadlock. Enter the counter plus 1, minus 1 after exiting.
/** * Synchronization method @author tomsnail * @date April 18, 2015 afternoon 12:15:30 * * Public synchronized void test1 () { test1 (); }
2). Fair/Non-fair
In the case of fairness, all threads that need to acquire a lock have the same chance to acquire a lock, whereas some threads have a priority to acquire the lock.
/*** Creates an instance of {@codeReentrantlock}. * This was equivalent to using {@codeReentrantlock (False)}. */ PublicReentrantlock () {Sync=NewNonfairsync (); } /*** Creates an instance of {@codeReentrantlock} with the * given fairness policy. * * @paramFair {@codetrue} If this lock should use a fair ordering policy*/ PublicReentrantlock (BooleanFair) {Sync= Fair?NewFairsync ():NewNonfairsync (); }
The construction method of the Reentrantlock lock provides a fair/unfair mechanism, while the synchronized does not have a definite fair/non-fairness guarantee, because it is the operating system on the line scheduling. The concrete realization and principle of fair lock and non-fair lock, then study well later.
4. Deadlock
Java because it is thread-level, I borrowed the deadlock of the process: it is a situation where multiple threads wait indefinitely for resources to be consumed by other threads.
The necessary conditions for deadlock generation:
a). Mutex conditions
A resource can only be consumed by one thread for a period of time
b). Non-preemption conditions
No other thread can preempt the resource until it is released
c). Occupancy and application conditions
The thread already occupies a resource, but requests a new resource, but the resource to be requested is occupied by another thread
d). Cyclic wait condition
Multiple threads waiting for other threads to release resources
Public classDeadlockthread {Static FinalString a = "a"; Static FinalString B = "B"; Public Static voidMain (string[] args) {Thread a=NewThread (NewDeadLockThread1 ()); Thread b=NewThread (NewDeadLockThread2 ()); A.start (); B.start (); }}classDeadLockThread1Implementsrunnable{@Override Public voidrun () {TESTB (); } Public voidTesta () {synchronized(DEADLOCKTHREAD.A) {System.out.println (DEADLOCKTHREAD.A); } } Public voidTestb () {synchronized(deadlockthread.b) {System.out.println (deadlockthread.b); Testa (); } } }classDeadLockThread2Implementsrunnable{@Override Public voidrun () {testa (); } Public voidTesta () {synchronized(DEADLOCKTHREAD.A) {System.out.println (DEADLOCKTHREAD.A); Testb (); } } Public voidTestb () {synchronized(deadlockthread.b) {System.out.println (deadlockthread.b); } } }
A deadlock occurs when the above four conditions are met at the same time.
When a deadlock occurs, we break down the four conditions above.
Re-learning Java Fundamentals (eight): The basics of Locks