交替执行AB线程让A线程+1,B线程-1

调用LockSupport方法实现


import java.util.concurrent.locks.LockSupport;

/**
 * およそ神
 * 交替执行AB线程
 */
public class Test1 {
    // 定义为static
    static Thread t1 = null;
    static Thread t2 = null;

    public static void main(String[] args) {
        Share share = new Share();
        t1 = new Thread(()->{
            for(int i = 0; i < 50; i++){
                try {
                    share.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LockSupport.unpark(t2); // 叫醒t2
                LockSupport.park(); // 阻塞当前线程t1
            }
        },"t1");

        t2 = new Thread(()->{
            for (int i = 0; i < 50; i++) {
                LockSupport.park(); // 阻塞当前线程t2
                try {
                    share.sub();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LockSupport.unpark(t1); // 叫醒t1
            }
        },"t2");
        t1.start();
        t2.start();
    }
    static class Share {
         int n = 0;
        synchronized void add() throws InterruptedException {
            String name = Thread.currentThread().getName();
            n += 1;
            System.out.println(name+"线程执行完add方法: "+n);
            Thread.sleep(50);
        }
        synchronized void sub() throws InterruptedException {
            String name = Thread.currentThread().getName();
            n -= 1;
            System.out.println(name+"线程执行完sub方法后n的值: "+n);
            Thread.sleep(50);
        }
    }


}

使用notify和wait实现


import java.util.concurrent.CountDownLatch;

/**
 * およそ神
 * 使用notify和wait
 */
public class CSpract {
    public static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) {
        Object lock = new Object();
        Share share = new Share();
        /**
         * synchronized的原理,多个线程同时抢一把锁,该锁维护一个队列(同步队列),没有争抢到锁的所有线程在同一个同步队列里。
         * wait:该线程释放锁后阻塞,该线程进入另一个队列(等待队列)
         * notify:随机叫醒等待队列中的任何一个线程
         * notifyAll: 唤醒等待队列中的所有线程,让他们去争夺同一把锁,谁争抢到谁执行。
         */
        Thread t1 = new Thread(() -> {
            try {
                synchronized (lock) {
                    countDownLatch.countDown(); //减去1
                    while (true) {
                        share.add();
                        lock.notify();//清空等待队列
                        lock.wait(); 
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                countDownLatch.await(); //等待,下面的代码一定不会先运行。这里使用的是await,不是wait
                synchronized (lock) {
                    while (true) {
                        share.sub();
                        lock.notify();//清空等待队列
                        lock.wait();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t2");
        t1.start();
        t2.start();
    }

    static class Share {
        int n = 0;

        synchronized void add() throws InterruptedException {
            String name = Thread.currentThread().getName();
            n += 1;
            System.out.println(name + "线程执行完add方法: " + n);
            Thread.sleep(50);
        }

        synchronized void sub() throws InterruptedException {
            String name = Thread.currentThread().getName();
            n -= 1;
            System.out.println(name + "线程执行完sub方法后n的值: " + n);
            Thread.sleep(50);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值