多线程加锁实现奇偶循环交替打印输出

package unmanned.supermarket.pay.controller;
public class test {
    private static int num = 100;
    public static void main(String[] args) {
        test test = new test();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(num > 0){
                    synchronized (test){
                        if (num %2 != 0){
                            try {
                                test.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println(Thread.currentThread().getName()+"票"+num);
                            num--;
                            test.notifyAll();
                        }
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    synchronized (test){
                        if (num %2 == 0){
                            try {
                                test.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println(Thread.currentThread().getName()+"票"+num);
                            num--;
                            test.notifyAll();
                        }
                    }
                }
            }
        }).start();
    }
}

猜你喜欢

转载自blog.csdn.net/potatoandpotato/article/details/80517660