ACM刷题
Java学习笔记
我们提供一个类:
class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print("foo");
}
}
public void bar() {
for (int i = 0; i < n; i++) {
print("bar");
}
}
}
两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。
请设计修改程序,以确保 "foobar" 被输出 n 次。
示例 1:
输入: n = 1
输出: "foobar"
解释: 这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
示例 2:
输入: n = 2
输出: "foobarfoobar"
解释: "foobar" 将被输出两次。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/print-foobar-alternately
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
code
方法一:采用加锁的形式
确保被锁住的区域在同一时刻只能被一个线程访问,再通一个变量来控制具体被哪个线程访问(状态切换),缺点,可能同一个线程多次持有锁,但当前状态不是该线程的访问状态,导致线程又退出去wait(),这样的重复操作浪费了CPU的资源
当然,这里也可以采用Lock锁来实现
class FooBar {
private int n;
private boolean fooTurn = true;
private Object lock = new Object();
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized(lock) {
if (!fooTurn) lock.wait();
fooTurn = false;
// printFoo.run() outputs "foo". Do not change or remove this line.
printFoo.run();
lock.notifyAll();
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized(lock) {
if (fooTurn) lock.wait(); // wait之后就得等待唤醒,然后再等待os调度
fooTurn = true;
// printBar.run() outputs "bar". Do not change or remove this line.
printBar.run();
lock.notifyAll();
}
}
}
}
信号量法
这也是一种比较常见的解法,在多线程共享一部分资源的时候,信号量提供了解决方法,这里每一时刻只用一个线程访问资源
import java.util.concurrent.Semaphore;
class FooBar {
private int n;
private Semaphore foo;
private Semaphore bar;
public FooBar(int n) {
this.n = n;
foo = new Semaphore(1); // 当前有一个可用信号量
bar = new Semaphore(0); // 当前有0个可用信号量
}
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();
}
}
}
生产者-消费者模型:阻塞列队实现
import java.util.concurrent.*;
class FooBar {
private int n;
private BlockingQueue<Integer> p = new ArrayBlockingQueue<>(1);
private BlockingQueue<Integer> t = new ArrayBlockingQueue<>(1);
public FooBar(int n) {
this.n = n;
try{
p.put(1);
}catch(InterruptedException e){
}
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
p.take(); // 我开所生产了
printFoo.run();
t.put(1); // 我生产完了
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) {
t.take(); // 我尝试开所消费
printBar.run();
p.put(1); // 我消费完了,你可以生产了
}
}
}
超时举例–无锁
无锁,但是会导致长时间占用CPU资源,出现许多循环轮空的状态,浪费计算资源,其他线程等待
class FooBar {
private int n;
public FooBar(int n) {
this.n = n;
}
volatile boolean permitFoo = true;
public void foo(Runnable printFoo) throws InterruptedException {
// 无锁,但是会导致长时间占用CPU资源,出现许多循环轮空的状态,浪费计算资源,其他线程等待
for (int i = 0; i < n; ) {
if(permitFoo) {
printFoo.run();
i++;
permitFoo = false;
}
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; ) {
if(!permitFoo) {
printBar.run();
i++;
permitFoo = true;
}
}
}
}