电影院多线程卖票,并计算不同电影票出售完成的时间实例

public class Ts {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        test();
 
    }
    private static void test() {
        Xc xc=new Xc();
        long startTime=System.currentTimeMillis();
        Thread t1=new Thread(xc);
        Thread t2=new Thread(xc);
        Thread t3=new Thread(xc);
        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");
        t1.start();
        t2.start();
        t3.start();
        while(t1.isAlive()||t2.isAlive()||t3.isAlive()) {    }
        long endTime=System.currentTimeMillis();
        System.out.println("当前程序耗时,"+(endTime-startTime)+"ms");   
    }
}
 
class Xc implements Runnable{
    private static int count=1000;
 
    @Override
    public synchronized void run() {
        // TODO Auto-generated method stub
        while(true) {
            if(count<=0) {
                break;
            }else {
                count--;
                System.out.println(Thread.currentThread().getName()+"卖出一张,剩余"
                        + count+"张");
            }
        }
         
    }
     
}

你可能感兴趣的:(其他,java)