Write a producer consumer example

Package com.shopping.test; 

public  class ShaoKao {
     // string 
    String Chuan; 

    // meat 
    String ROU; 

    // barbecue state, the initial state is not barbecue 
    Boolean In Flag = to false ; 

    public String getChuan () {
         return Chuan; 
    } 

    public  void setChuan (String Chuan) {
         the this .chuan = Chuan; 
    } 

    public String getRou () {
         return ROU; 
    } 

    public  void setRou (String ROU) {
         the this .rou = rou;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}

The following is a producer

Package Penalty for com.shopping.test; 

/ * 
 * Note: 
 * barbecue food goods shop threads and thread relationship ---> Communication (exclusive) 
 * synchronization technology must be used to ensure that only one in two threads execution 
 must ensure that the only lock object * , you can use the barbecue object as a lock object 
 * barbecue food goods shop class and the class will need to pass in the barbecue object as a parameter 
 * 1. the need to create the position of a member of a barbecue variable 
 * 2. arguments constructor, assigned to the variable barbecue 
 * / 
public  class ShaoKaoPu the extends the Thread {
     // 1. members need to create a barbecue variable position as the lock object 
    Private ShaoKao BZ; 

    // 2. use constructor parameters, assigned to the variable barbecue 
    public ShaoKaoPu (ShaoKao BZ) {
         the this = .bz BZ; 
    } 

    // set the task thread (run), the production of barbecue 
    public  void RUN () {
        // define a variable that decision barbecue stuffed in the end what to do 
        int COUNT = 0 ;
         // endless loop, so that the shop has been producing barbecue grill 
        the while ( to true ) {
             // must be used to ensure synchronization technology can only have two threads a performing 
            the synchronized (bz) {
                 // state judge barbecue 
                IF (bz.flag == to true ) {
                     // have a barbecue, do not produce, barbecue shop call wait method into a wait state 
                    the try { 
                        bz.wait ( ); 
                    } the catch (InterruptedException E) { 
                        e.printStackTrace (); 
                    } 
                } 
                //After being awakened food goods, there is no proof of barbecue, but also began producing barbecue
                 // add some fun: the production of alternate barbecue 
                IF (COUNT% 2 == 0 ) {
                     // produce lamb skewers 
                    bz.chuan = "big string" ; 
                    bz .rou = "lamb" ; 
                } the else {
                     // produce beef onion stuffing 
                    bz.chuan = "big string" ; 
                    bz.rou = "lamb" ; 
                } 
                COUNT ++ ; 
                System.out.println ( "barbecue shop is producing:" + bz.chuan + bz.rou + "barbecue" );
                //Production takes 3 seconds to barbecue 
                the try { 
                    the Thread.sleep ( 3000 ); 
                } the catch (InterruptedException E) { 
                    e.printStackTrace (); 
                } 
                // barbecue grill good production shop, modification status of true barbecue has 
                bz.flag = true ;
                 // wake up and eat barbecue food goods threads 
                bz.notify ();
                 // barbecue shop has produced good barbecue 
                System.out.println ( "good barbecue shop has produced a" + bz.chuan + bz.rou + "barbecue" + "can begin to eat food goods" ); 
            } 
        } 

    } 
}

consumer

Package com.shopping.test; 

public  class ChiHuo the extends the Thread { 

    // 1. members need to create a barbecue variable position as the lock object 
    Private ShaoKao BZ; 

    // 2. Use constructor parameters, assigned to the variable barbecue 
    public ChiHuo (ShaoKao bz) {
         the this .bz = bz; 
    } 
    // set the thread task (run), eat barbecue 
    public  void RUN () {
         // infinite loop, so eat barbecue goods have been eating 
        the while ( to true ) {
             // must use synchronization technology to ensure that two threads only one execution in 
            the synchronized (BZ) {
                 // state judgment barbecue 
                IF (== bz.flagto false ) {
                     // find no grill, food goods call wait method enters a wait state and so do the barbecue grill laying 
                    the try { 
                        bz.wait (); 
                    } the catch (InterruptedException E) { 
                        e.printStackTrace (); 
                    } 
                } 
                // is barbecue after wake-up shop, eat barbecue proven 
                System.out.println ( "food goods was eating:" + bz.chuan + bz.rou + "barbecue" );
                 // food goods eating barbecue
                 // modify barbecue is false no 
                bz.flag = false ;
                 // eat barbecue goods shop thread wake, continue to produce barbecue 
                bz.notify ();
                System.out.println ( "food goods have been put:" + bz.chuan + bz.rou + "barbecue finished" ); 
                System.out.println ( "-------------- -------------------------------------------------- ----------- " ); 
            } 
        } 
    } 
}

 

test:

Package Penalty for com.shopping.test; 

public  class the Test { 

    public  static  void main (String [] args) {
         // create an object barbecue, as the lock object 
        ShaoKao bz = new new ShaoKao ();
         // create a thread barbecue shop 
        new new ShaoKaoPu (bz) .start ();
         // open food goods threads 
        new new ChiHuo (bz) .start (); 
    } 
}

 

Output is: 

    

BBQ Shop is producing: a large string of grilled lamb
barbecue shop has produced a large string of good mutton barbecue food goods can begin to eat
food goods are eating: a large string of mutton barbecue
food goods have been put: a large string of mutton barbecue finished
---------- -------------------------------------------------- ---------------
barbecue shop is producing: a large string of grilled lamb
barbecue shop has produced a large string of good mutton barbecue food goods can begin to eat
food goods are eating: a large string of mutton barbecue
food goods have been put: big string lamb barbecue finished
--------------------------------------------- ------------------------------
barbecue shop is producing: a large string of grilled lamb
barbecue shop has produced a large string of good mutton barbecue food goods can begin eat
eat eat goods are: a large string of mutton barbecue
food goods have been put: a large string of mutton barbecue finished
------------------------------ ---------------------------------------------

 

  

Guess you like

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