LeetCode多线程题目——交替打印FooBar(用等待通知机制构造屏障)

在这里插入图片描述
想要交替输出,两边线程就需要通信,因为只有两个线程分别调用两个方法,所以仅需要使用一个布尔类型的量作为信号即可,用flag的两种状态来分别限制两者,如果为真,foo线程等待 ,bra执行,执行完毕唤醒foo
如果为假,bar线程等待,foo执行,执行完毕唤醒bar
以下分别ReentrantLock和synchronized代码块来实现。

class FooBar {
    private int n;
    private ReentrantLock lock = new ReentrantLock();
    private volatile boolean flag = false; 
    private Condition wait = lock.newCondition();
    public FooBar(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {
       
        for (int i = 0; i < n; i++) {
            lock.lock();
            try{
                while(flag){
                    wait.await();
                }
                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
                flag = true;
                wait.signal();
            }finally{
                lock.unlock();
            }
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
            lock.lock();
            try{
                while(!flag){
                    wait.await();
                }
                
                // printBar.run() outputs "bar". Do not change or remove this line.
        	    printBar.run();
                flag = false;
                wait.signal();
            }finally{
                lock.unlock();
            }
            
        }
    }
}
class FooBar {
    private int n;
    volatile boolean flag = false;
    public FooBar(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {
       
        for (int i = 0; i < n; i++) {
            synchronized(FooBar.class){
                while(flag){
                    FooBar.class.wait();
                }
                // printFoo.run() outputs "foo". Do not change or remove this line.
                printFoo.run();
                flag = true;
                FooBar.class.notify();
            }   
        }

    }

    public void bar(Runnable printBar) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
           synchronized(FooBar.class){
                while(!flag){
                    FooBar.class.wait();
                }
                
                // printBar.run() outputs "bar". Do not change or remove this line.
        	    printBar.run();
                flag = false;
                FooBar.class.notify();
            }
        }
    }
}

由于仅有两种状态两个线程,仅用一个volatile变量+sleep操作也不是不可以。毕竟这里的赋值操作时本身是原子的。
不过会比等待通知的写法慢,因为这里是存在两个线程来竞争且等待时间比较短用的是轻量级的锁(运气贼好的情况还可能用偏向锁(如果是那基本上阔以去买彩票了2333)),而第二种方法我人为的等待了1ms,所以会相对较慢。

class FooBar {
    private int n;
    volatile boolean flag = false;
    public FooBar(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {
       
        for (int i = 0; i < n; i++) {
                while(flag)Thread.sleep(1);
                printFoo.run();
                atomicBoolean = true;
             
        }

    }

    public void bar(Runnable printBar) throws InterruptedException {
        
        for (int i = 0; i < n; i++) {
                while(!flag)Thread.sleep(1);
        	    printBar.run();
                atomicBoolean = false;
        }
        
    }
}

或者使用信号量(本质上是一个限定获取次数的共享锁(重入次数有限))。
慢于等待通知,快于第二种。

class FooBar {
    private int n;
    private Semaphore foo = new Semaphore(1);
    private Semaphore bar = new Semaphore(0);
    public FooBar(int n) {
        this.n = n;
    }

    public void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
                foo.acquire();
                printFoo.run();
                bar.release();
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
                bar.acquire();
                printBar.run();
                foo.release();
        }
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值