其他分享
首页 > 其他分享> > 力扣多线程练习----交替打印FooBar

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

作者:互联网

交替打印FooBar


题目


两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。

请设计修改程序,以确保 “foobar” 被输出 n 次。
在这里插入图片描述

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");
    }
}

标签:int,FooBar,void,InterruptedException,----,new,多线程,public
来源: https://blog.csdn.net/qq_22130209/article/details/115041803