java两个线程交替打印0~100

  • Post author:
  • Post category:java


1、(不推荐)单单使用synchronized锁代码块,会出现同一个线程一直抢到锁,而另一个线程一直没有拿到,就会导致线程做很多无谓的空转。

 private int count = 0;
 private final Object lock = new Object();

 public void turning() {
     Thread even = new Thread(() -> {
         while (count < 100) {
             synchronized (lock) {
                 // 只处理偶数
                 if ((count & 1) == 0) {
                     System.out.println(Thread.currentThread().getName() + ": " + count++);
                 }
             }
         }
     }, "偶数");
     Thread odd = new Thread(() -> {
         while (count < 100) {
             synchronized (lock) {
                 // 只处理奇数
                 if ((count & 1) == 1) {
                     System.out.println(Thread.currentThread().getName() + ": " + count++);
                 }
             }
         }
     }, "奇数");
     even.start();
     odd.start();
}

2、使用synchronized锁住object对象,使用object对象的notifyAll和wait方法。线程1打印之后唤醒其他线程,然后让出锁,自己进入休眠状态。因为进入了休眠状态就不会与其他线程抢锁,此时只有线程2在获取锁,所以线程2必然会拿到锁。线程2以同样的逻辑执行,唤醒线程1并让出自己持有的锁,自己进入休眠状态。这样来来回回,持续执行直到任务完成。就达到了两个线程交替获取锁的效果了。


public class TurningRunner{
    public static int count = 1;
    public static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(new changePrintRunner(),"奇数").start();
        Thread.sleep(1);
        new Thread(new changePrintRunner(),"偶数").start();
    }
}
class changePrintRunner implements Runnable{
    @Override
    public void run(){
        while (TurningRunner.count<=100){
            //获取锁
            synchronized (TurningRunner.lock){
                //拿到锁就打印
                System.out.println(Thread.currentThread().getName()+" "+TurningRunner.count++);
                //唤醒其他线程
                TurningRunner.lock.notifyAll();
                try{
                    if(TurningRunner.count<=100){
                        TurningRunner.lock.wait();//当前线程等待
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}



版权声明:本文为qq_38765865原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。