【Java】两个线程循环打印1-10

有点类似于之前写过的Java实现生产者消费者模式,这里继续来巩固下。首先是使用synchronized方式来实现。

public class Main {
    
    

    private static volatile int number = 0;  
    private static final Object object = new Object();  // 对象锁

    public static void main(String[] args) {
    
    
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                do {
    
    
                    synchronized (object) {
    
    
                        while (number % 2 == 0) {
    
    
                            try {
    
    
                                object.wait();
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
    
    
                                e.printStackTrace();
                            }
                        }
                        System.out.println("odd : " + number++);
                        object.notifyAll();
                    }
                } while (number < 10);
            }
        }).start();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                do {
    
    
                    synchronized (object) {
    
    
                        while (number % 2 != 0) {
    
    
                            try {
    
    
                                object.wait();
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
    
    
                                e.printStackTrace();
                            }
                        }
                        System.out.println("even : " + number++);
                        object.notifyAll();
                    }
                } while (number < 10);
            }
        }).start();


    }
}

结果:
在这里插入图片描述

使用ReentrantLock实现如下:

public class Main {
    
    

    private static volatile int number = 0;
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition oddC = lock.newCondition();
    private static Condition evenC = lock.newCondition();

    public static void main(String[] args) {
    
    
        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                do {
    
    
                    lock.lock();
                    try{
    
    
                        // 同步代码块
                        while(number % 2 == 0){
    
      // 这里是用while,不使用if,还是为了防止虚假唤醒
                            oddC.await();
                        }
                        System.out.println("odd : " + number++);
                        evenC.signal();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    } finally {
    
    
                        lock.unlock();
                    }
                } while (number < 10);
            }
        }).start();

        new Thread(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                do {
    
    
                    lock.lock();
                    try{
    
    
                        while(number % 2 != 0){
    
    
                            evenC.await();
                        }
                        System.out.println("even : " + number++);
                        oddC.signal();
                    } catch (InterruptedException e) {
    
    
                        e.printStackTrace();
                    } finally {
    
    
                        lock.unlock();
                    }
                } while (number < 10);
            }
        }).start();
    }
}

结果:
在这里插入图片描述


References

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/120301271
今日推荐