세 개의 창구에서 동시에 표를 팔아서 Runnable 방식을 실현하다

7493 단어 라인
package com.hp.zuoye
public class chepaio implements Runnable {
      // 
    public static int count=100;
      // 
    public static Object object=new Object();
    @Override
    public void run() {
       // 
            try {
           //   
                while (true){
                    Thread.sleep(2000);
         //synchronized   , .   , 
                synchronized (object){

                    if (count>0){
                        System.out.println(" :"+count+" ");
                        count--;// 1  , 1 
                    }else{
                        System.out.println(" ");
                        System.exit(0); // 
                    }

                }
            }


        } catch (InterruptedException e) {
                e.printStackTrace();
            }

    }
    public static void main(String[] args) {
        chepaio cp1=new chepaio();
        chepaio cp2=new chepaio();
        chepaio cp3=new chepaio();
        // 
        Thread thread1=new Thread(cp1);
        Thread thread2=new Thread(cp2);
        Thread thread3=new Thread(cp3);
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

좋은 웹페이지 즐겨찾기