Java multi-thread design: Wait, y, yyall, and synchronized usage Mechanism
Wait, y, yyall, and synchronized usage mechanisms:
Synchronized(OBJ ){While(!Condition) {obj. Wait ();} obj. dosomething ();}
After thread a acquires the OBJ lock, it finds that the condition is not met and cannot continue the next processing. Therefore, thread a will wait () and discard the object lock.
Then, in another thread B, if B modifies some conditions so that the condition of thread a is met, it can wake up thread:
Synchronized(OBJ) {Condition=True; Obj. Y ();}
Note the following concepts:
# The OBJ lock must be obtained before calling the wait () and notify () Methods of OBJ, that is, it must be written in synchronized (OBJ ){...}Code.
# After calling obj. Wait (), thread a releases the OBJ lock. Otherwise, thread B cannot obtain the OBJ lock and cannot get the OBJ lock in synchronized (OBJ ){...} Wake up a in the code segment.
# After the obj. Wait () method is returned, thread a needs to obtain the OBJ lock again to continue execution.
# If both A1, A2, and A3 are in OBJ. wait (), B calls obj. notify () can only wake up one of A1, A2, and A3 (which one is determined by JVM ).
# Obj. notifyall () can all wake up A1, A2, A3, but continue to execute obj. the next statement of wait () must obtain the OBJ lock. Therefore, only one of A1, A2, and A3 has the opportunity to obtain the lock for further execution,
For example, A1, the rest must wait for A1 to release the OBJ lock before execution can continue.
# When B calls obj. Y/policyall, B is holding the OBJ lock. Therefore, although A1, A2, and A3 are awakened, they still cannot obtain the OBJ lock. After B exits the synchronized block and releases the OBJ lock,
One of A1, A2, and A3 has the opportunity to obtain the lock and continue execution.
Original article address:
Http://blog.csdn.net/super_level/article/details/7833800
Wait, y, yyall, and synchronized usage mechanisms:
Synchronized(OBJ ){While(!Condition) {obj. Wait ();} obj. dosomething ();}
After thread a acquires the OBJ lock, it finds that the condition is not met and cannot continue the next processing. Therefore, thread a will wait () and discard the object lock.
Then, in another thread B, if B modifies some conditions so that the condition of thread a is met, it can wake up thread:
Synchronized(OBJ) {Condition=True; Obj. Y ();}
Note the following concepts:
# The OBJ lock must be obtained before calling the wait () and notify () Methods of OBJ, that is, it must be written in synchronized (OBJ ){...} Within the code segment.
# After calling obj. Wait (), thread a releases the OBJ lock. Otherwise, thread B cannot obtain the OBJ lock and cannot get the OBJ lock in synchronized (OBJ ){...} Wake up a in the code segment.
# After the obj. Wait () method is returned, thread a needs to obtain the OBJ lock again to continue execution.
# If both A1, A2, and A3 are in OBJ. wait (), B calls obj. notify () can only wake up one of A1, A2, and A3 (which one is determined by JVM ).
# Obj. notifyall () can all wake up A1, A2, A3, but continue to execute obj. the next statement of wait () must obtain the OBJ lock. Therefore, only one of A1, A2, and A3 has the opportunity to obtain the lock for further execution,
For example, A1, the rest must wait for A1 to release the OBJ lock before execution can continue.
# When B calls obj. Y/policyall, B is holding the OBJ lock. Therefore, although A1, A2, and A3 are awakened, they still cannot obtain the OBJ lock. After B exits the synchronized block and releases the OBJ lock,
One of A1, A2, and A3 has the opportunity to obtain the lock and continue execution.
Original article address:
Http://blog.csdn.net/super_level/article/details/7833800