From 956c3d5d46a38ccf2cf71bdcd3dd8f654acb5e57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E9=A1=BA=E5=B9=B3?= <1078094401@qq.com> Date: Fri, 10 Jan 2020 20:25:45 +0800 Subject: [PATCH] 059_week05 --- ...\233\277\346\211\223\345\215\260FooBar.md" | 38 ++++++++ ...16\345\245\207\345\201\266\346\225\260.md" | 86 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 "week_05/59/\344\272\244\346\233\277\346\211\223\345\215\260FooBar.md" create mode 100644 "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" 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 0000000..5a497f4 --- /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 0000000..3d4215f --- /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(); + } + } + } + } +``` + -- Gitee