其他分享
首页 > 其他分享> > 多线程卖票-demo

多线程卖票-demo

作者:互联网

 

public static void main(String[] args) {
    ThreadDemo2 runnal = new ThreadDemo2();
    for (int i = 0; i < 3; i++) {
        new Thread(runnal,"线程" + i).start();
    }
}

class ThreadDemo2 implements Runnable {
    private int ticket = 1000;
    Lock lock = new ReentrantLock();
    Condition con = lock.newCondition();
    @Override
    public void run()  {
        while (true) {
            lock.lock();
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "#####" + --ticket);
            }
            lock.unlock();
        }
    }
}

 

标签:卖票,demo,int,lock,new,ticket,多线程,runnal,ThreadDemo2
来源: https://blog.csdn.net/weixin_39690861/article/details/114629402