Wait state thread

Six kinds of thread state:

  NEW: has not yet started threads
  RUNNABLE: java thread being executed in a virtual machine
  BLOCKER: by blocking and waiting for a monitor lock thread
  TIMED_WAITING: within a specified waiting time is in a dormant state WAITING: dormant indefinitely  TERMINATED: thread that has exited
  

Definitions: Waiting state introduced in the API as follows: First Ge are waiting indefinitely for another thread execution thread wakes up action . In fact, here it involves the knowledge of the inter-thread communication - wait wake-up mechanism. 
For example,
to create a thread Customers (consumers): the boss to inform the type and quantity of barbecue, call the wait () method, renounce the use of the implementation of the cpu, into WAITING state (wait indefinitely)
to create a boss thread (Producer): Flower 5 seconds to do barbecue, well after the barbecue, call notify () method, wake-up customers to eat barbecue
attention:
the customer and the boss threads must use synchronization block wrapped up, to ensure that there is a wait and wake up only in the implementation of
the simultaneous use of the lock object must ensure the uniqueness
only lock object can call wait and notify method
method Object class
  void wait (): calling this subject in other threads notify before () method or the notifyAll () method causes the current thread to wait
  void wait (long miles) : there are parameters, then, is timed wait, the time comes, do not be automatically wake up wake up, jump RUNNABLE or BLOCKED state
  void notify (): wake up a single thread waiting on this object's monitor, if there are multiple threads, randomly wake a
  void notifyAll (): wake up all threads waiting on this object's monitor

following a direct look Test code type:
Package Penalty for com.shopping.test; 

public  class WaitAndNotify { 
    public  static  void main (String [] args) {
         // create the lock object, the only guarantee 
        Final Object obj = new new Object ();
         // create a thread Customers 
        new new the Thread () {
             public  void RUN () {
                 // ensure thread waiting to wake up and only one execution, requires the use of synchronous technology 
                the synchronized (obj) { 
                    System.out.println ( "customer: barbecue to inform the owner of the types and quantities" );
                     // call the wait method, gives up the CPU into an infinite wait wAITING state 
                    the try {
                        // compile abnormal, but not declared with throws, as parent class does not run throw exception statement, nor subclass 
                        obj.wait (); 
                    } the catch (InterruptedException E) { 
                        e.printStackTrace (); 
                    } 
                    // is after the code wake 
                    System.out.println ( "customer: open eat" ); 
                } 
            } 
        } .start (); 

        // create a thread boss (producer) 
        new new the thread () {
             public  void RUN () {
                 // boss It took 5 seconds to do barbecue 
                the try { 
                    the Thread.sleep ( 5000 ); 
                }the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
                the synchronized (obj) {
                     // barbecue has already done, wake customer eating barbecue 
                    System.out.println ( "Boss: barbecue've done" );
                     // call notify () method, the thread wake customer 
                    obj.notify (); 
                } 
            } 
        } .start (); 
    } 

    // Description : the boss just started threads have been sleeping, it is certainly the customer into the first thread synchronization code block, the customer first start thread execution 

  

}
Output: 
    Customer: the boss to inform the type and quantity of barbecue 
    boss: Barbecue has already done the 
    customer: open to eat 
 this is a direct test of the test class, specific projects have to be executed in the case of

Guess you like

Origin www.cnblogs.com/wyf-love-dch/p/11407311.html