Thread waiting wake

What is waiting for wake-up mechanism

This is a among multiple threads coordination mechanism. We often think of the thread comes between threads is competition (Race) , such as a lock to compete, but this is not the whole story, there will be a cooperation mechanism between threads. Like in the company that you and your colleagues, you may be present at the promotion of competition, but more often you more to work together to accomplish certain tasks.

After the thread is in a predetermined operation, enters the standby state ( the wait () ), waiting for their execution after the completion of the specified code then other threads its wake ( the Notify () ); when there are multiple threads waiting, if necessary, use notifyAll () to wake up all waiting threads.

wait / notify is a collaborative mechanism among threads.

Waiting in the wake method

Wait for the wake-up mechanism is used to solve the problem of communication between threads, meaning the use of three methods are as follows:

  1. wait: the thread is no longer active, no longer involved in scheduling, into the wait set, so do not waste CPU resources, not to compete lock, then thread state that is WAITING. It is also waiting for another thread to perform a particular action , that is, " notice (the Notify) " on this object to wait for the release of the thread from the wait set out to re-enter the scheduling queue (ready queue) in

  2. notify: notify a thread object wait set is selected in the release; for example, the location of restaurants available, customers wait dining oldest first seated.

  3. notifyAll: All threads on the notification object wait set is released.

note:

Even if only to inform a waiting thread, the thread can not be notified to resume execution immediately, because it had interrupted the place is in sync blocks, but at the moment it no longer holds the lock, so she needs to try to acquire the lock again (probably faced with competition from other threads) to resume execution in place after the original method after a successful call to wait.

Summarized as follows:

  • If you can get a lock, the thread becomes RUNNABLE status from WAITING state;

  • Otherwise, set out from the wait, and into the entry set, the thread from WAITING state and becomes BLOCKED state

Call wait and notify methods need to pay attention to the details

  1. wait and notify methods method must be called by the same lock object. Because: the corresponding lock object can be woken notify the use of threads after the wait method call with a lock object.

  2. notify and wait method is a method belonging to the class of Object. Because: lock object can be any object, any object which belongs to the class are inherited Object class.

  3. notify and wait method must be used in the method of synchronization code block or a synchronizing function. Because: must call this method through two lock object.

example

Package com.thread; 


public  class baozi { 

    public String xianer; 

    public String Pier; 

    Boolean   In Flag = to false ; // whether the resource exists steamed buns resource status 

    public  static  void main (String [] args) { 
        baozi baozi = new new baozi (); 
        baoZiPu BaoZiPu = new new BaoZiPu ( "Bao Zipu" , baozi); 
        chiHuo chiHuo = new new chiHuo ( "food goods" , baozi); 

        baoZiPu.start (); 

        chiHuo.start (); 



    } 
}
package com.thread;


public class ChiHuo  extends Thread{

    private BaoZi baozi;

    public ChiHuo(String name, BaoZi baozi) {
        super(name);
        this.baozi = baozi;
    }

    @Override
    public void run() {
       while(true){
           synchronized (baozi){
               if(baozi.flag==false){//没有包子
                   try {

                       baozi.wait();
                   } the catch (InterruptedException E) { 
                       e.printStackTrace (); 
                   } 

               } 
               // have bun 
               System.out.println ( "food goods are bun" ); 
               baozi.flag = to false ; 
               baozi.notify (); 

           } 
       } 

    } 
}
package com.thread;


public class BaoZiPu extends Thread {
    private BaoZi baozi;

    public BaoZiPu(String name, BaoZi baozi) {
        super(name);
        this.baozi = baozi;
    }

    @Override
    public void run() {
        int count=0;
        //没有包子造
        while(true){
            synchronized (baozi){
                if(baozi.flag==true){
                    try {
                        baozi.wait (); 
                    } the catch (InterruptedException E) { 
                        e.printStackTrace (); 
                    } 
                } 
                // do not start making buns 
                System.out.println ( "Start Bao Zipu baking" ); 
                baozi.flag = to true ; 
                Baozi. Notify (); 

            } 

        } 

        


    } 
}

 

Guess you like

Origin www.cnblogs.com/liushisaonian/p/11281013.html