力扣多线程练习----交替打印FooBar

交替打印FooBar


题目

力扣多线程练习----交替打印FooBar
两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。

请设计修改程序,以确保 “foobar” 被输出 n 次。
力扣多线程练习----交替打印FooBar

Semaphore

代码

import java.util.concurrent.Semaphore;

public class FooBar {
    private int n;

    public FooBar(int n) {
        this.n = n;
    }
    Semaphore semaphoreFoo=new Semaphore(1);
    Semaphore semaphoreBar=new Semaphore(0);
    public void foo(Runnable printFoo) throws InterruptedException {

        for (int i = 0; i < n; i++) {

            semaphoreFoo.acquire();
            printFoo.run();
            semaphoreBar.release();
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {

        for (int i = 0; i < n; i++) {

            semaphoreBar.acquire();
            printBar.run();
            semaphoreFoo.release();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        FooBar fooBar=new FooBar(10);
        new Thread(()->{
            try {
                fooBar.foo(new PrintFoo());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                fooBar.bar(new PrintFar());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }

}
class PrintFoo implements Runnable{

    @Override
    public void run() {
        System.out.print("foo");
    }
}
class PrintFar implements Runnable{

    @Override
    public void run() {
        System.out.print("bar");
    }
}

Lock公平锁

public class FooBar {
    private int n;

    public FooBar(int n) {
        this.n = n;
    }
    Lock lock=new ReentrantLock(true);
    static volatile boolean flag=true;
    public void foo(Runnable printFoo) throws InterruptedException {

        for (int i = 0; i < n; ) {
            lock.lock();
            try {
                if(flag){
                    printFoo.run();
                    i++;
                    flag=false;
                }
            }finally {
                lock.unlock();
            }
        }
    }

    public void bar(Runnable printBar) throws InterruptedException {

        for (int i = 0; i < n; ) {
            lock.lock();
            try {
                if(!flag){
                    printBar.run();
                    i++;
                    flag=true;
                }
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        FooBar fooBar=new FooBar(10);
        new Thread(()->{
            try {
                fooBar.foo(new PrintFoo());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                fooBar.bar(new PrintFar());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }

}
class PrintFoo implements Runnable{

    @Override
    public void run() {
        System.out.print("foo");
    }
}
class PrintFar implements Runnable{

    @Override
    public void run() {
        System.out.print("bar");
    }
}
上一篇:生产者和消费者问题


下一篇:Java~多线程算法实战题目之商店售票