Synchronous Synchronized Method and code block

Source: Internet
Author: User

In Java, the wait and lock mechanisms are closely related. For example:

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 is wait ().

In another thread B, if B modifies some conditions so that the condition of thread a is met, thread A can be awakened:

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 the code segment of synchronized (OBJ.

# Call obj. after wait (), thread a releases the OBJ lock. Otherwise, thread B cannot obtain the OBJ lock, and it cannot be 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 can be executed only after A1 releases the OBJ lock.

# 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, only one of A1, A2, and A3 will have the opportunity to obtain the lock and continue execution.


Wait (), wait (long), Y (), policyall () and other methods are the instance methods of the current class,

Wait () is used to release the lock for the thread holding the object lock;
Wait (long) is used to obtain the lock again after the thread holding the object lock is released for long (milliseconds). Wait () is equivalent to wait (0;
Y () is to wake up a thread waiting for the lock of this object. If there are more than one waiting thread, the wake-up thread is determined by JVM;
Yyall is used to wake up all threads waiting for the lock of this object. The notifyall () method should be used first, because it is easier for the JVM to find the most appropriate thread to wake up than to wake up a thread.

1. Where synchronized exists, wait and notify may not be available.

2. with wait, synchronized must be used in zooy. this is because wait and notify are not thread classes, but methods that every object has. Moreover, these two methods are related to the object lock. Where there is a lock, there must be synchronized.

In addition, please note: If you want to put Y and wait methods together, you must first call notify and then call wait. Because after wait is called, this thread is no longer the current thread.

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: 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.