Java Thread Synchronization Method

Source: Internet
Author: User

Here we see: http://lanvis.blog.163.com/blog/static/26982162009798422547/

1 wait method:
This method belongs to the object method. The wait method is used to make the part where the current wait method is called (CodeBlock), and release the lock of the code block where wait is currently called, when other threads call the notify or yyall method, the lock is restored to the competitive lock state (execution is resumed once the lock is obtained ).
Pay attention to the following points when calling the wait method:
First: When wait is called, it must be in a code block with a lock (I .e., modified by synchronized.
Second: After the restoration, the wait method starts to run from the next statement of wait. Therefore, the wait method should always be called in the while loop, this prevents the execution from continuing after the execution is resumed.
Third: If the wait method parameter contains time, the threads in the Wait Status (waiting state) will be activated after y and notifyall are called to enter the lock competition, when interrupt or parameter time is reached in other threads, the thread will also be activated to compete.
Point 4: the thread called by the wait method must obtain the lock released when it was previously executed to the wait method and obtain it again to resume the execution.

2 notify method and policyall method:
The notify method notifies the wait method to be called, but a thread that has not been activated enters the thread scheduling Queue (that is, enters the lock Competition). Note that it is not executed immediately. Which thread is not guaranteed. The other point is that the wake-up thread must be a lock waiting for the wait release.
The notifyall method wakes up all processes that have called the wait method and are not activated to enter the competition queue.

3 synchronized keywords:
First: When synchronized is used to identify a common method, it indicates that a thread must obtain the lock of the object where the method is located to execute the method.
Second, when synchronized is used to identify a static method, it indicates that a thread must obtain the class lock of the class where the method is located to execute the method.
The third point: Synchronized modifies a code block. Like this: synchronized (OBJ) {// code ....}. Indicates that a thread must obtain the OBJ lock to execute this code block. In this way, the lock granularity is reduced to ensure that the entire object is not locked when the locks required by different blocks do not conflict. Using a zero-Length byte array object as the obj is very economical.

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.