LeetCode(多线程)- 1115. 交替打印 FooBar

Posted 程序员牧码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(多线程)- 1115. 交替打印 FooBar相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

题目大意:略。

解题思路:略。

相关企业

  • 阿里巴巴
  • 亚马逊(Amazon)

AC 代码

// 手太阴肺经 BLOCKING Queue
public class FooBar 
    private int n;
    private BlockingQueue<Integer> bar = new LinkedBlockingQueue<>(1);
    private BlockingQueue<Integer> foo = new LinkedBlockingQueue<>(1);
    public FooBar(int n) 
        this.n = n;
    
    public void foo(Runnable printFoo) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            foo.put(i);
            printFoo.run();
            bar.put(i);
        
    

    public void bar(Runnable printBar) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            bar.take();
            printBar.run();
            foo.take();
        
    


// 手阳明大肠经CyclicBarrier 控制先后
class FooBar6 
    private int n;

    public FooBar6(int n) 
        this.n = n;
    

    CyclicBarrier cb = new CyclicBarrier(2);
    volatile boolean fin = true;

    public void foo(Runnable printFoo) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            while(!fin);
            printFoo.run();
            fin = false;
            try 
                cb.await();
             catch (BrokenBarrierException e) 
        
    

    public void bar(Runnable printBar) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            try 
                cb.await();
             catch (BrokenBarrierException e) 
            printBar.run();
            fin = true;
        
    


// 手少阴心经 自旋 + 让出CPU
class FooBar5 
    private int n;

    public FooBar5(int n) 
        this.n = n;
    

    volatile boolean permitFoo = true;

    public void foo(Runnable printFoo) throws InterruptedException      
        for (int i = 0; i < n; ) 
            if(permitFoo) 
        	    printFoo.run();
            	i++;
            	permitFoo = false;
            else
                Thread.yield();
            
        
    

    public void bar(Runnable printBar) throws InterruptedException        
        for (int i = 0; i < n; ) 
            if(!permitFoo) 
        	printBar.run();
        	i++;
        	permitFoo = true;
            else
                Thread.yield();
            
        
    


// 手少阳三焦经 可重入锁 + Condition
class FooBar4 
    private int n;

    public FooBar4(int n) 
        this.n = n;
    
    Lock lock = new ReentrantLock(true);
    private final Condition foo = lock.newCondition();
    volatile boolean flag = true;
    public void foo(Runnable printFoo) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            lock.lock();
            try 
            	while(!flag) 
                    foo.await();
                
                printFoo.run();
                flag = false;
                foo.signal();
            finally 
            	lock.unlock();
            
        
    

    public void bar(Runnable printBar) throws InterruptedException 
        for (int i = 0; i < n;i++) 
            lock.lock();
            try 
            	while(flag) 
                    foo.await();
            	
                printBar.run();
                flag = true;
                foo.signal();
            finally 
            	lock.unlock();
            
        
    


// 手厥阴心包经 synchronized + 标志位 + 唤醒
class FooBar3 
    private int n;
    // 标志位,控制执行顺序,true执行printFoo,false执行printBar
    private volatile boolean type = true;
    private final Object foo=  new Object(); // 锁标志

    public FooBar3(int n) 
        this.n = n;
    
    public void foo(Runnable printFoo) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            synchronized (foo) 
                while(!type)
                    foo.wait();
                
                printFoo.run();
                type = false;
                foo.notifyAll();
            
        
    

    public void bar(Runnable printBar) throws InterruptedException 
        for (int i = 0; i < n; i++) 
            synchronized (foo) 
                while(type)
                    foo.wait();
                
                printBar.run();
                type = true;
                foo.notifyAll();
            
        
    


// 手太阳小肠经 信号量 适合控制顺序
class FooBar2 
    private int n;
    private Semaphore foo = new Semaphore(1);
    private Semaphore bar = new Semaphore(0);
    public FooBar2(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();
        
    

以上是关于LeetCode(多线程)- 1115. 交替打印 FooBar的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(多线程)- 1115. 交替打印 FooBar

leetcode中等1115交替打印 FooBar

阿里真题:线程交叉打印

阿里真题:线程交叉打印

LeetCode(多线程)- 题集

LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口