Home » Java » Java Communication between threads (wait / notify ...

Java Communication between threads (wait / notify mechanism)

thread to start running, has its own stack space, like a script code, terminated in accordance with the implementation of the step by step until. However, each running thread, if only to run in isolation, so there is not much value, but if multiple threads can cooperate with each other to complete the work, it will bring value.

great

, and Java multithreading waiting and notification mechanism is used to complete communication between threads,.


a thread to modify a value of an object, and another thread to perceive the change, then the corresponding operation, the whole process begins with a thread, and the final implementation is another thread. The former is the producer, the consumer, the mode of isolation "how to do". "What to do, in the function level to achieve decoupling, the system structure has good scalability. This method realized the simplest mechanism is to allow consumers to check whether the variable cycle thread to keep in line with expectations, as shown in the following code:


while (value, =desire) {
Xxx.wait ();
}
doSomething ();

in the while loop does not meet the conditions, if the conditions are satisfied out of the while loop, thus completing the work. Let the consumer thread consumer wait is put into the WAITING state, when the producer thread updates the value of value is notify wake up the thread, let the consumer thread to complete doSomething.


sample code:


public class WaitNotify{
static, boolean, flag = true;
static, Object, lock = new, Object ();
public, static, void, main (String[], args) throws Exception{
Thread, waitThread = new, Thread (new, Wait (), , WaitThread, );
WaitThread.start ();
TimeUtil.SECONDS.sleep (1);
Thread, notifyThread = new, Thread (new, Notify (), , NotifyThread, );
NotifyThread.start ();
}
static, class, Wait, implements, Runnable{
public, void, run () {
// lock
Synchronized (lock) {
// when conditions are not satisfied, enter the WAITTING state and release the lock lock
while (flag) {
System.out.println (, flag, is, true, );
Lock.wait ();
}
// condition satisfies
System.out.println (, doSomething, );
}
}
}
static, class, Notify, implements, Runnable{
public, void, run () {
// lock
Synchronized (lock) {
// gets the lock of the lock, and then notices that the lock lock will not be released
// the WaitThread thread is returned to the method from the wait method until the thread of the notification is executed and the lock lock is released
Lock.notifyAll ();
System.out.println (, flag, is, false, now, );
Flag = false;
}
}
}

output reads as follows:


flag is true


flag, is, false, now,


doSomething


now, let's look at the call wait (), notify (), notifyAll () Note:



    When
  • calls wait (), notify (), notifyAll (), you need to lock the calling object first,.

  • After the
  • calls the wait () method, the thread is changed from RUNNING to WAITING, and the current thread is placed in the object waiting queue

  • After the
  • notify (), notifyAll () method is called, the thread waits until the wait () method is returned, but when the thread that calls notify () and notifyAll () releases the lock, the thread waits until the thread has the opportunity to return.
  • from wait
    (
  • notify) method in the waiting queue waiting for a thread from the waiting queue to synchronous queue, and (notifyAll) method is all waiting threads from the waiting queue to synchronous queue, the thread is moved from WAITING to BLOCKED
  • state
  • returns from the wait () method on the premise that the lock.
  • of the calling object is obtained

now we can see that the wait / notification mechanism relies on the synchronization mechanism, which is designed to ensure that when the thread is returned from the wait () method, it is aware that the notification thread modifies the variables.


The classic paradigm of waiting / notification

we refine the classic paradigm of waiting / notifications in the example code above, which is divided into two parts, namely, the waiting party (consumer) and the Notifying Party (producer)


the waiting party principle:



  • 1) get the lock
  • of the object
    (
  • 2) if the condition is not satisfied, then the calling wait () method of the call causes the thread to enter the waiting, and the condition
  • is still checked after being notified
  • conditional satisfaction performs the corresponding logical


pseudo code:


 synchronized (object) {
while (condition not satisfied) {
Object.Wait ();
}
Corresponding logical processing
}

notification principle:



  • 1) get the lock
  • of the object
  • 2) change the condition

  • 3) notify all threads
  • waiting on that object

pseudo code:


 synchronized (object) {
change a condition
Object.NotifyAll ();
}

Latest