diff --git "a/week_05/59/\344\272\244\346\233\277\346\211\223\345\215\260FooBar.md" "b/week_05/59/\344\272\244\346\233\277\346\211\223\345\215\260FooBar.md" new file mode 100644 index 0000000000000000000000000000000000000000..5a497f460c40de24ea740cbb64878c4eb74ee642 --- /dev/null +++ "b/week_05/59/\344\272\244\346\233\277\346\211\223\345\215\260FooBar.md" @@ -0,0 +1,38 @@ +```java +static class FooBar { + int n; + volatile boolean p = false; + public void foo(Runnable printFoo) throws InterruptedException { + for (int i = 0; i < n; i++) { + synchronized (this) { + while (p) { + this.wait(); + } + //printFoo.run(); + System.out.print("foo"); + p = true; + this.notify(); + } + } + } + + public void bar(Runnable printBar) throws InterruptedException { + for (int i = 0; i < n; i++) { + synchronized (this) { + while (!p) { + this.wait(); + } + System.out.print("bar"); + //printBar.run(); + p = false; + this.notify(); + } + } + } + public FooBar(int n){ + this.n = n; + + } + } +``` + diff --git "a/week_05/59/\346\211\223\345\215\260\351\233\266\344\270\216\345\245\207\345\201\266\346\225\260.md" "b/week_05/59/\346\211\223\345\215\260\351\233\266\344\270\216\345\245\207\345\201\266\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..3d4215f24c26cb6276cab52d89f799dcc21a6192 --- /dev/null +++ "b/week_05/59/\346\211\223\345\215\260\351\233\266\344\270\216\345\245\207\345\201\266\346\225\260.md" @@ -0,0 +1,86 @@ +```java + static class ZeroEvenOdd { + private int n; + volatile private int num = 1; + volatile private boolean zero = true; + public ZeroEvenOdd(int n) { + this.n = n; + } + + // printNumber.accept(x) outputs "x", where x is an integer. + public void zero(IntConsumer printNumber) throws InterruptedException { + while(num<=n) { + //System.out.println("打印0方法"+num+" "+n); + synchronized (this) { + while (!this.zero) { + //System.out.println("零"); + //System.out.println("zero是false,0方法等待"); + //if(this.num>=n) break; + if(num>=n) return; + this.wait(); + } + //if(this.num>=n) break; + //System.out.println("zero是true,0方法打印"); + //zero为true进来 + printNumber.accept(0); + this.zero = false; + this.notifyAll(); + } + } + } + + public void even(IntConsumer printNumber) throws InterruptedException { + synchronized (this){ + while(num<=n) { + /// System.out.println("打印偶数方法"+num+" "+n); + while ((this.num & 1) == 1 || this.zero) {//num是奇数则等待释放 + //System.out.println("num是奇数,偶数方法等待"); + if(this.num>n) break; + this.wait(); + } + if(this.num>n) break; + //System.out.println("num是偶数,偶数方法往下走"); + // while (this.zero) { // 如果zero是true,循环等待 + //System.out.println("zero是true,偶数方法等待"); + // this.wait(); + //} + //zero是false,等待num + //System.out.println("zero是false,偶数方法执行"); + //zero为false并且num为偶数打印 + printNumber.accept(num); + ++num; + this.zero = true; + this.notifyAll(); + } + } + } + + public void odd(IntConsumer printNumber) throws InterruptedException { + synchronized (this) { + while (num <= n) { + //System.out.println("打印奇数方法"+num+" "+n); + while ((this.num & 1) == 0 || this.zero) { + //System.out.println("偶数"); + //System.out.println("num是偶数,奇数方法等待"); + if(this.num>n) break; + this.wait(); + } + if(this.num>n) break; + //System.out.println("num是奇数,奇数方法往下走"); +// while (this.zero) {//如果zero是true,循环等待 +// // System.out.println("zero是true,奇数方法等待"); +// this.wait(); +// } + //zero是false,等待num + //System.out.println("zero是false,奇数方法执行"); + //zero为false并且num为奇数 打印 + printNumber.accept(num); + ++num; + this.zero = true; + this.notifyAll(); + } + } + } + } +``` +