Java Thread Programming 1.8.1-Inter-thread Commu

Source: Internet
Author: User
Tags sleep
The Need for Inter-thread Signaling
Through synchronization, one thread can safely change values that another thread will read. How does the second thread know that the values have changed? What if the second thread is waiting for the values to change by rereading the values every few seconds?
One not-so-good way that a thread can wait for a value to change is by using a busy/wait:
While (getValue ()! = DesiredValue ){
Thread. sleep (500 );
}
Such code is called a busy/wait because the thread is busy using up processor resources to continually check to see if the value has changed. to use fewer resources, the sleep time cocould be increased, but then the thread might not find out about the change for quite some time. on the other hand, if the sleep time is already CED, the thread will find out sooner, but will waste even more of the processor resources. in Java, there is a much better way to handle this kind of situation: the wait/policy mechanic.
Sometimes we need communication between threads. For example, how does the second thread know that some values of the first thread have changed? This is not a good method. It is called busy/wait. if the sleep () test value changes, it will occupy processor resources, and the cycle frequency is not easy to grasp, which is a waste of resources and slow down the response speed. In this case, java provides a better solution: wait/notify mechanism
The Wait/Notify mechanic
The wait/notify mechanic allows one thread to wait for a notification from another thread that it may proceed.
Minimal Wait/Notify
At a bare minimum, you need an object to lock on and two threads to implement the wait/notify mechanic.
Imagine that there is a member variable, valueLock, that will be used for synchronization:
Private Object valueLock = new Object ();
The first thread comes along and executes this code fragment:
Synchronized (valueLock ){
Try {
ValueLock. wait ();
} Catch (InterruptedException x ){
System. out. println ("interrupted while waiting ");
Related Article

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.