Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b1910e

Browse files
committed
feat: add leetcode question #1195
1 parent 734eb07 commit 9b1910e

File tree

5 files changed

+545
-0
lines changed

5 files changed

+545
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1195;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.concurrent.locks.Condition;
5+
import java.util.concurrent.locks.Lock;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
import java.util.function.IntConsumer;
8+
9+
/**
10+
* <pre>
11+
* author: dhl
12+
* date : 2020年11月6日
13+
* desc :
14+
* </pre>
15+
*/
16+
17+
class FizzBuzzLock {
18+
private int n;
19+
private Lock lock = new ReentrantLock();
20+
private Condition conNumber = lock.newCondition();
21+
private Condition conFizz = lock.newCondition();
22+
private Condition conBuzz = lock.newCondition();
23+
private Condition conFizzBuzz = lock.newCondition();
24+
25+
public FizzBuzzLock(int n) {
26+
this.n = n;
27+
}
28+
29+
// printFizz.run() outputs "fizz".
30+
public void fizz(Runnable printFizz) throws InterruptedException {
31+
try {
32+
lock.lock();
33+
for (int i = 3; i <= n; i += 3) {
34+
if (i % 5 != 0) {
35+
conFizz.await();
36+
printFizz.run();
37+
conNumber.signalAll();
38+
}
39+
40+
}
41+
} finally {
42+
lock.unlock();
43+
}
44+
}
45+
46+
// printBuzz.run() outputs "buzz".
47+
public void buzz(Runnable printBuzz) throws InterruptedException {
48+
try {
49+
lock.lock();
50+
for (int i = 5; i <= n; i += 5) {
51+
if (i % 3 != 0) {
52+
conBuzz.await();
53+
printBuzz.run();
54+
conNumber.signalAll();
55+
}
56+
}
57+
} finally {
58+
lock.unlock();
59+
}
60+
}
61+
62+
// printFizzBuzz.run() outputs "fizzbuzz".
63+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
64+
try {
65+
lock.lock();
66+
for (int i = 15; i <= n; i += 15) {
67+
conFizzBuzz.await();
68+
printFizzBuzz.run();
69+
conNumber.signalAll();
70+
}
71+
} finally {
72+
lock.unlock();
73+
}
74+
}
75+
76+
// printNumber.accept(x) outputs "x", where x is an integer.
77+
public void number(IntConsumer printNumber) throws InterruptedException {
78+
try {
79+
lock.lock();
80+
for (int i = 1; i <= n; i++) {
81+
if (i % 3 == 0 && i % 5 == 0) {
82+
conFizzBuzz.signalAll();
83+
conNumber.await();
84+
} else if (i % 3 == 0) {
85+
conFizz.signalAll();
86+
conNumber.await();
87+
} else if (i % 5 == 0) {
88+
conBuzz.signalAll();
89+
conNumber.await();
90+
} else {
91+
printNumber.accept(i);
92+
}
93+
}
94+
} finally {
95+
lock.unlock();
96+
}
97+
}
98+
99+
public static void main(String... args) {
100+
FizzBuzzLock fizzBuzzLock = new FizzBuzzLock(15);
101+
Thread thd = new Thread(() -> {
102+
try {
103+
fizzBuzzLock.number(value -> System.out.print(value + " "));
104+
} catch (Exception e) {
105+
e.printStackTrace();
106+
}
107+
});
108+
109+
Thread tha = new Thread(() -> {
110+
try {
111+
fizzBuzzLock.fizz(() -> System.out.print("fizz "));
112+
} catch (Exception e) {
113+
}
114+
});
115+
Thread thb = new Thread(() -> {
116+
try {
117+
fizzBuzzLock.buzz(() -> System.out.print("buzz "));
118+
} catch (Exception e) {
119+
120+
}
121+
});
122+
123+
Thread thc = new Thread(() -> {
124+
try {
125+
fizzBuzzLock.fizzbuzz(() -> System.out.print("buzz "));
126+
} catch (Exception e) {
127+
128+
}
129+
});
130+
tha.start();
131+
thb.start();
132+
thc.start();
133+
thd.start();
134+
}
135+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.hi.dhl.algorithms.other.concurrency._1195;
2+
3+
import java.util.concurrent.Semaphore;
4+
import java.util.function.IntConsumer;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2020年11月6日
10+
* desc :
11+
* </pre>
12+
*/
13+
class FizzBuzzSemaphore {
14+
private int n;
15+
private Semaphore sNumber = new Semaphore(1);
16+
private Semaphore sFizzBuzz = new Semaphore(0);
17+
private Semaphore sBuzz = new Semaphore(0);
18+
private Semaphore sFizz = new Semaphore(0);
19+
volatile int value = 0;
20+
21+
public FizzBuzzSemaphore(int n) {
22+
this.n = n;
23+
}
24+
25+
// printFizz.run() outputs "fizz".
26+
public void fizz(Runnable printFizz) throws InterruptedException {
27+
for (int i = 3; i <= n; i += 3) {
28+
if (i % 5 != 0) {
29+
sFizz.acquire();
30+
printFizz.run();
31+
sNumber.release();
32+
}
33+
34+
}
35+
}
36+
37+
// printBuzz.run() outputs "buzz".
38+
public void buzz(Runnable printBuzz) throws InterruptedException {
39+
for (int i = 5; i <= n; i += 5) {
40+
if (i % 3 != 0) {
41+
sBuzz.acquire();
42+
printBuzz.run();
43+
sNumber.release();
44+
}
45+
46+
}
47+
}
48+
49+
// printFizzBuzz.run() outputs "fizzbuzz".
50+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
51+
for (int i = 15; i <= n; i += 15) {
52+
sFizzBuzz.acquire();
53+
printFizzBuzz.run();
54+
sNumber.release();
55+
}
56+
}
57+
58+
// printNumber.accept(x) outputs "x", where x is an integer.
59+
public void number(IntConsumer printNumber) throws InterruptedException {
60+
for (int i = 1; i <= n; i++) {
61+
sNumber.acquire();
62+
if (i % 3 == 0 && i % 5 == 0) {
63+
sFizzBuzz.release();
64+
} else if (i % 3 == 0) {
65+
sFizz.release();
66+
} else if (i % 5 == 0) {
67+
sBuzz.release();
68+
} else {
69+
printNumber.accept(i);
70+
sNumber.release();
71+
}
72+
}
73+
}
74+
75+
public static void main(String... args) {
76+
FizzBuzzSemaphore fizzBuzz = new FizzBuzzSemaphore(15);
77+
Thread tha = new Thread(() -> {
78+
try {
79+
fizzBuzz.fizz(() -> System.out.print("fizz "));
80+
} catch (Exception e) {
81+
82+
}
83+
});
84+
Thread thb = new Thread(() -> {
85+
try {
86+
fizzBuzz.buzz(() -> System.out.print("buzz "));
87+
} catch (Exception e) {
88+
89+
}
90+
});
91+
92+
Thread thc = new Thread(() -> {
93+
try {
94+
fizzBuzz.fizzbuzz(() -> System.out.print("fizzbuzz "));
95+
} catch (Exception e) {
96+
97+
}
98+
});
99+
100+
Thread thd = new Thread(() -> {
101+
try {
102+
fizzBuzz.number(value1 -> System.out.print(value1 + " "));
103+
} catch (Exception e) {
104+
105+
}
106+
});
107+
108+
tha.start();
109+
thb.start();
110+
thc.start();
111+
thd.start();
112+
}
113+
114+
}

‎offer/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [多线程-交替打印FooBar](/multi-thread/04-print-alternately.md)
1313
* [多线程-打印零与奇偶数](/multi-thread/05-print-zero-even-odd.md)
1414
* [多线程-H2O 生成](/multi-thread/06-h20.md)
15+
* [多线程-交替打印字符串](/multi-thread/07-fizz-buzz.md)
1516

1617
* 大厂面试题
1718

‎offer/menu.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* [多线程-按序打印](/multi-thread/03-print-in-order.md)
1010
* [多线程-交替打印FooBar](/multi-thread/04-print-alternately.md)
1111
* [多线程-打印零与奇偶数](/multi-thread/05-print-zero-even-odd.md)
12+
* [多线程-H2O 生成](/multi-thread/06-h20.md)
13+
* [多线程-交替打印字符串](/multi-thread/07-fizz-buzz.md)
1214

1315
* 大厂面试题
1416

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /