java problems of producers and consumers

TestThread Package; 

public  class the Test3 {
     public  static  void main (String [] args) { 
        Clerk C = new new Clerk ();
         // not spending time without the production of consumption, production
         // Manufacturer 
        new new the Thread ( new new the Runnable () {     
            @ override 
            public  void RUN () {
                 // the TODO Auto-Generated Method Stub 
                the synchronized (C) {
                     // infinite production of 
                    the while ( to true ) {
                         IF (c.productNum == 0) {
                             // begin production of 
                            the System. OUT .println ( " product is 0, the start of production " );
                             the while (c.productNum < . 4 ) { 
                                c.productNum ++ ; 
                                the System. OUT .println ( " Product Inventory: " + C .productNum); 
                            } 
                            . the System OUT .println ( " product number: " + c.productNum + " , the end of production .");
                             // wake consumer 
                            c.notify (); 
                        } the else {
                             the try {
                                 // Manufacturer waiting 
                                c.wait (); 
                            } the catch (InterruptedException E) {
                                 // the TODO Auto-Generated Block the catch 
                                e.printStackTrace (); 
                            } 
                        } 
                    } 
                } 
                
            } 
        }, "Producers " ) .start ();
         // consumers 
        new new the Thread ( new new Runnable () {     
            @Override 
            public  void RUN () {
                 // TODO Auto-Generated Stub Method, 
                the synchronized (c) {
                     the while ( to true ) {
                         IF (c == .productNum 4 ) { 
                            . System OUT .println ( " inventory is 4, start spending " );
                             the while (c.productNum> 0 ) {
                                c.productNum - ; 
                                System. OUT .println ( " inventory: " + c.productNum); 
                            } 
                            . System OUT .println ( " Product number: " + c.productNum + " , the end consumer " );
                             // wake producers 
                            c.notify (); 
                        } the else {
                             the try {
                                 // consumers waiting
                                c.wait();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }
                
            }
        },"消费者").start();
    }
}
class Clerk{
    public static int productNum = 0;
}

Part of the output:

The product is 0 and began production
inventory: 1
item in stock: 2
item in stock: 3
item in stock: 4
Product number: 4, the end of production
inventory is 4, start consumer
product Availability: 3
item in stock: 2
item in stock: 1
Product Inventory : 0
product number: 0, end consumer
products to zero to begin production of
inventory: 1
item in stock: 2
item in stock: 3
item in stock: 4
product number: 4, the end of production

Guess you like

Origin www.cnblogs.com/xiximayou/p/12076936.html