Analog buying

com.aaa.thread.Test Package; 

public  class Test1 the implements the Runnable {
     Private  int NUM = 10 ;
     Private  int COUNT = 0 ; 

    // must start method to run the run method 
    @Override
     public  void run () {
         the while ( to true ) {
             IF (! ) Buy {()
                 BREAK ; 
            } 
            the try { 
                the Thread.sleep ( 100 ); 
            } the catch (InterruptedException E) {
                e.printStackTrace (); 
            } 
        } 
    } 
    // the synchronized sync 
    public the synchronized Boolean Buy () {
         IF (NUM <= 0 ) {
             return  to false ; 
        } 

        NUM - ; 
        COUNT ++ ; 

        the System. OUT .println (the Thread. . currentThread () getName () + " snapped to the clothes ," + " left " + NUM + " member " );
         return  to true ; 
    }
 // threaded foolproof
    public static void main(String[] args) {
        Test1 t = new Test1();
        Thread t1 = new Thread(t,"One");
        Thread t2 = new Thread(t,"Two");
        Thread t3 = new Thread(t,"Three");
        Thread t4 = new Thread(t,"Four");
        Thread t5 = new Thread(t,"Five");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

    }
}

 

Guess you like

Origin www.cnblogs.com/fanqiexin/p/11124282.html