线程协作工具类:控制并发流程: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;
}
}
}