Java multi-thread design: Wait, y, yyall, and synchronized usage Mechanism

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.