Lock with unlock

xiaoxiao2021-03-06  15

Package test;

Public class tiger3 {static class inner {private thread thread = null; private int count = 0; public synchronized void getLock () {while () {// Unlimited loop, if no other thread is released Wake up it, it never quits because it will not terminate the While loop yourself. When other thread calls notify () released, it will continue to perform this WAIT (), which will again call the trygetlock () method again to try to get the lock. System.out.println (thread.currentthread (). GetName () "is waited!"); Try {wait ();} catch (interruptedException e) {// Todo: Handle Exception} system.out.println (Thread.Println .currentthread (). GetName () "is notified!");}}

/ *

It is assumed that after the thread gets the lock, the thread two attempts to obtain the lock, when thread is pointing to the first thread, so conditional (thread == null) does not match. At this time, because thread.currentthread () points to thread two, thread points to thread one, so conditional 2 (thread == thread.currentthread ()) does not match. The Wait () method in getLock () will then be called, and the current thread (thread b) will temporarily fall into the wait () state. The thread is activated until the thread is called after the thread is completed.

So the true intent of this mechanism is to achieve the purpose of the scheduling thread by controlling the reference to the current thread handle. So it is not an account or ATM object, but the current thread itself. * / Private synchronized boolean trygetlock () {boilean istrue = false; if (thread == null) {// A reference to the current thread. Thread = thread.currentthread (); istrue = true;} If (thread == thread.currentthread ()) {// This condition is set to simulate the current thread that has been locked again to obtain a lock effort. For example, an ATM is repeated several times to access an account several times. If this condition is not set, when a thread first calls getLock () gets the trivial, then call the getLock () attempt again, then the wait () method will be executed, the lock will be incorrectly released. If all threads are executed so, all threads are likely to occur in Wait (). Istrue = true; count ;} return istrue;} public synchronized void freeelock () {if (thread == thread.crentthread ()) {count-; // Simulation current thread After repeated try to get the lock, repeated trial release lock.

IF (count == 0) {thread = null; notify (); system.out.println (thread.currentthread (). getname () "call notify ()!");}}} public synchronized void m () {System.out.println (thread.currentthread (). GetName () "begin m ()"); try {thread.sleep (1000);} catch (interruptedExcection E) {} system.out.println (thread. CurrentThread (). getName () "end m ()");}} public static void main (string [] args) {final inner in = new inner (); thread [] TS = new Thread [10]; for (INT i = 0; i

T0.Start () *********** T0 Begin M () T1.Start () ************ T2.Start () ****** ****** T3.Start () *********** T4.Start () ************ T5.Start () ***** ******* T6.Start () ************ T7.Start () ************ T8.Start () **** ******** T9.Start () *********** T0 end m () T1is waited! T2is waited! T3is waited! T4is waited! T5is waited! T6is waited! T7is waited T8is waited! T0 begin m () t0 end m () t0 call notify ()! T1 is notified! T1 begin m () t0.end () T1 end m () T1 begin m () T1 END M () T1 Call Notified! T2 Begin M () T1.End () T2 end M () T2 BeGin M () T2 end m () T2 Call Notify ()! T3 is notified! t3 begin m () t2.end () T3 End M ( T3 begin m () T3 end m () T3 call notify ()! T4 is notified! T4 begin m () t3.end () T4 end M () T4 BeGin M () T4 end m () T4 call notified! t5 begin m () T4.End () T5 end m () T5 Begin M () T5 end m () t5 call notify ()! T6 is notified! T6 begin m () T5.End () T6 end m () T6 Begin M () T6 End M () T6 Call Notify! T7 Begin M () T6 .end () T7 End M () T7 Begin M () T7 End M () T7 Call Notify ()! T7.END () T8 IS Notified! T8 Begin M () T8 End M () T8 Call Notify ()! T8.End () T9 IS Notified! T9 begin m () T9 end m () T9 begin m () T9 end m () T9 call notify ()! T9.END ()

Analysis:

Ten threads are almost started, but after the thread 0 gets the lock, the other nine will be blocked immediately. Until the thread 0 is executed and release the lock, and use the notify () method to wake up one of the threads in the nine waities. As a result, Tn.End () has some misplacement, because before printing this line content, thread TN has released the lock, and printing the content of this line It is not in the Synchronized block, so it is non-threaded.

If the modification code becomes like this:

private synchronized boolean tryGetLock () {boolean istrue = false; if (thread == null) {thread = Thread.currentThread (); istrue = true;} // if (thread == Thread.currentThread ()) {// istrue = true; // count ; //} returnizide;} public synchronized void freeelock () {if (thread == thread.currentthread ()) {// count -; // if (count == 0) {Thread = null; notify (); system.out.println (thread.currentthread (). GetName () "call notify ()!"); //}}}

T0.Start () *********** T0 Begin M () T1.Start () ************ T2.Start () ****** ****** T3.Start () *********** T4.Start () ************ T5.Start () ***** ******* T6.Start () ************ T7.Start () ************ T8.Start () **** ******** T9.Start () *********** T0 end m () T1is waited! T2is waited! T3is waited! T4is waited! T5is waited! T6is waited! T7is waited ! T8is waited! T9is waited! T0is waited!

Analysis: Because T0 repeat call getLock () does not release the lock, it is finally caused by all threads to be blocked, caught in the "deadlock" state.

If the modified code is as follows:

For (int i = 0; i

result:

T0.START () *********** T0 Begin M1 () T1.Start () ************ T2.Start () ****** ****** T3.Start () *********** T4.Start () ************ T5.Start () ***** ******* T6.Start () ************ T7.Start () ************ T8.Start () **** ******** T9.Start () ************ T0 end m1 () T1is waited! T2is waited! T3is waited! T4 waiting! T5is waited! T6is waited! T7is waited T8is waited! T0 Begin M3 () T0 () t0 () T0 Call Notify ()! T1 IS Notified! T1 Begin M1 () T0.End () T1 End M1 () T1 Begin M2 () T1 End M2 () T1 Begin M3 () T1 End M3 () T1 Begin M4 () T1 END M4 () T1 Call Notify ()! T2 is Notified! T2 Begin M1 () T1.End () T2 End M1 () T2 Begin M2 () T2 End M2 () T2 Begin M3 () T2 End M3 () T2 Begin M4 () T2 End M4 () T2 Call Notify ()! T3 is Notified! T3 Begin M1 () T2.End () T3 End M1 () T3 Begin M2 () T3 End M2 () T3 Begin M3 () T3 End M4 () T3 Call Notify ()! T4 is Notified T3.end () T4 END M1 () T4 Begin M2 () T4 End M2 () T4 Begin M3 () T4 END M3 () T4 B EGIN M4 () T4 END M4 () T4 Call Notify ()! T5 is Notified! T4.End () T5 Begin M1 () T5 END M1 () T5 Begin M2 () T5 END M2 () T5 END M3 () T5 Begin M4 () T5 END M4 () T5 Call Notify ()! T6 is Notified! T6 Begin M1 () T5.End () T6 End M1 () T6 Begin M2 () T6 End M2 () T6 Begin M3 () T6 End M3 () T6 Begin M4 () T6 End M4 () T6 Call Notify () T7 begin m1 () t6.end ()

T7 begin M2 () T7 END M2 () T7 Begin M3 () T7 END M3 () T7 begin M4 () T7 end m4 () T7 Call Notify ()! T8 is Notified! T8 Begin M1 () T7.End () T8 End M1 () T8 Begin M2 () T8 END M2 () T8 Begin M3 () T8 END M3 () T8 Begin M4 () T8 end M4 () T8 Call Notify ()! T9 is Notified! T9 Begin M1 () T8.End () T9 End M1 () T9 Begin M2 () T9 End M2 ( ) T9 begin m3 () T9 end m3 () T9 begin M4 () T9 end m4 () T9 Call Notify ()! T9.End () Analysis:

First, it can be seen clearly, other threads cannot be exposed to these four methods during the four methods of TN call. This is the effect that Synchronized statement blocks cannot be achieved: Synchronized can protect other threads in the process of performing a synchronous step block, but when the thread performs this synchronization block, you want to continue executing other When you synchronize? At this time, other threads suddenly ran over to take the lock. The Lock () and UNLOCK () mechanisms are to solve this problem.

Second, because the Lock () and UNLOCK () mechanisms are not ordinary objects, but the thread itself, the M1 (), M2 (), M3 (), M4 () is Synchronized, there is no major relationship. This is because any thread wants to perform these four methods must be "licensed" by getLock (). And within the same period of time, only one thread can get this four ways to call this "license".

转载请注明原文地址:https://www.9cbs.com/read-46496.html

New Post(0)