1115. 交替打印FooBar

https://leetcode-cn.com/problems/print-foobar-alternately/ 

线程协作工具类:控制并发流程:https://blog.csdn.net/qq_40794973/article/details/104111726 


1 Thread.yield() 

class FooBar {
    private int n;
    private volatile boolean flag;
    public FooBar(int n) {
        this.n = n;
    }
    public void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            while(flag){
                Thread.yield();
            }
            printFoo.run();
            flag = true;
        }
    }
    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            while(!flag){
                Thread.yield();
            }
            printBar.run();
            flag = false;
        }
    }
}

2 synchronized 

class FooBar {
    private int n;
    private volatile boolean flag;
    public FooBar(int n) {
        this.n = n;
    }
    public synchronized void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            if (flag) {
                this.wait();
            }
            printFoo.run();
            flag = true;
            this.notify();
        }
    }
    public synchronized void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            if (!flag) {
                this.wait();
            }
            printBar.run();
            flag = false;
            this.notify();
        }
    }
}

3 ReentrantLock + Condition 

class FooBar {
    private int n;
    private volatile boolean flag;
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = 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 {
                if (flag) {
                    condition.await();
                }
                printFoo.run();
                flag = true;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
    }
    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            lock.lock();
            try {
                if (!flag) {
                    condition.await();
                }
                printBar.run();
                flag = false;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
    }
}

4 Semaphore

两个Semaphore互相牵制

class FooBar {
    private int n;
    private Semaphore fooSemaphore = new Semaphore(1, true);
    private Semaphore barSemaphore = new Semaphore(0, true);
    public FooBar(int n) {
        this.n = n;
    }
    // printFoo.run() outputs "foo". Do not change or remove this line.
    public void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            fooSemaphore.acquire();
            printFoo.run();
            barSemaphore.release();
        }
    }
    // printBar.run() outputs "bar". Do not change or remove this line.
    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            barSemaphore.acquire();
            printBar.run();
            fooSemaphore.release();
        }
    }
}

 

class FooBar {
    private int n;
    private volatile boolean foo;
    //公平非公平对结果没有影响
    private Semaphore semaphore = new Semaphore(1);
    public FooBar(int n) {
        this.n = n;
    }
    // printFoo.run() outputs "foo". Do not change or remove this line.
    public void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            semaphore.acquire();
            printFoo.run();
            foo = true;
        }
    }
    // printBar.run() outputs "bar". Do not change or remove this line.
    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            while (!foo) {}
            printBar.run();
            foo = false;
            semaphore.release();
        }
    }
}

 

//public static void main(String[] args) {
//    FooBar fooBar = new FooBar(4);
//    new Thread(() -> {
//        try {
//            fooBar.bar(() -> System.out.print("bar "));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }).start();
//    new Thread(() -> {
//        try {
//            fooBar.foo(() -> System.out.print("foo "));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//    }).start();
//}

 

 

 

 

 

两个死循环不行 死循环【错误方法后面分析】

//错误方法后面分析
class FooBar {
    private int n;
    private volatile boolean foo;
    public FooBar(int n) {
        this.n = n;
    }
    // printFoo.run() outputs "foo". Do not change or remove this line.
    public void foo(Runnable printFoo) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            while (foo) {}
            printFoo.run();
            foo = true;
        }
    }
    // printBar.run() outputs "bar". Do not change or remove this line.
    public void bar(Runnable printBar) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            while (!foo) {}
            printBar.run();
            foo = false;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值