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

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();
                }
            }
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值