File tree 10 files changed +371
-68
lines changed
1115.Print FooBar Alternately
10 files changed +371
-68
lines changed Original file line number Diff line number Diff line change @@ -65,22 +65,96 @@ public class Foo {
65
65
66
66
<!-- 这里可写通用的实现逻辑 -->
67
67
68
+ 用两个锁或信号量分别锁住 ` second ` 和 ` third ` ,在 ` first ` 执行完后释放 ` second ` 的锁,` second ` 执行完后释放 ` third ` 的锁
69
+
68
70
<!-- tabs:start -->
69
71
70
72
### ** Python3**
71
73
72
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
73
75
74
76
``` python
75
-
77
+ class Foo :
78
+ def __init__ (self ):
79
+ self .l2 = threading.Lock()
80
+ self .l3 = threading.Lock()
81
+ self .l2.acquire()
82
+ self .l3.acquire()
83
+
84
+ def first (self , printFirst : ' Callable[[], None]' ) -> None :
85
+ printFirst()
86
+ self .l2.release()
87
+
88
+ def second (self , printSecond : ' Callable[[], None]' ) -> None :
89
+ self .l2.acquire()
90
+ printSecond()
91
+ self .l3.release()
92
+
93
+ def third (self , printThird : ' Callable[[], None]' ) -> None :
94
+ self .l3.acquire()
95
+ printThird()
76
96
```
77
97
78
98
### ** Java**
79
99
80
100
<!-- 这里可写当前语言的特殊实现逻辑 -->
81
101
82
102
``` java
103
+ class Foo {
104
+
105
+ private final Semaphore s2 = new Semaphore (0 );
106
+ private final Semaphore s3 = new Semaphore (0 );
107
+
108
+ public Foo () {
109
+ }
110
+
111
+ public void first (Runnable printFirst ) throws InterruptedException {
112
+ printFirst. run();
113
+ s2. release();
114
+ }
115
+
116
+ public void second (Runnable printSecond ) throws InterruptedException {
117
+ s2. acquire();
118
+ printSecond. run();
119
+ s3. release();
120
+ }
121
+
122
+ public void third (Runnable printThird ) throws InterruptedException {
123
+ s3. acquire();
124
+ printThird. run();
125
+ }
126
+ }
127
+ ```
83
128
129
+ ### ** C++**
130
+
131
+ ``` cpp
132
+ class Foo {
133
+ private:
134
+ mutex m2, m3;
135
+
136
+ public:
137
+ Foo() {
138
+ m2.lock();
139
+ m3.lock();
140
+ }
141
+
142
+ void first(function<void()> printFirst) {
143
+ printFirst();
144
+ m2.unlock();
145
+ }
146
+
147
+ void second (function<void()> printSecond) {
148
+ m2.lock();
149
+ printSecond();
150
+ m3.unlock();
151
+ }
152
+
153
+ void third(function<void()> printThird) {
154
+ m3.lock();
155
+ printThird();
156
+ }
157
+ };
84
158
```
85
159
86
160
### **...**
Original file line number Diff line number Diff line change @@ -51,13 +51,85 @@ public class Foo {
51
51
### ** Python3**
52
52
53
53
``` python
54
-
54
+ class Foo :
55
+ def __init__ (self ):
56
+ self .l2 = threading.Lock()
57
+ self .l3 = threading.Lock()
58
+ self .l2.acquire()
59
+ self .l3.acquire()
60
+
61
+ def first (self , printFirst : ' Callable[[], None]' ) -> None :
62
+ printFirst()
63
+ self .l2.release()
64
+
65
+ def second (self , printSecond : ' Callable[[], None]' ) -> None :
66
+ self .l2.acquire()
67
+ printSecond()
68
+ self .l3.release()
69
+
70
+ def third (self , printThird : ' Callable[[], None]' ) -> None :
71
+ self .l3.acquire()
72
+ printThird()
55
73
```
56
74
57
75
### ** Java**
58
76
59
77
``` java
78
+ class Foo {
79
+
80
+ private final Semaphore s2 = new Semaphore (0 );
81
+ private final Semaphore s3 = new Semaphore (0 );
82
+
83
+ public Foo () {
84
+ }
85
+
86
+ public void first (Runnable printFirst ) throws InterruptedException {
87
+ printFirst. run();
88
+ s2. release();
89
+ }
90
+
91
+ public void second (Runnable printSecond ) throws InterruptedException {
92
+ s2. acquire();
93
+ printSecond. run();
94
+ s3. release();
95
+ }
96
+
97
+ public void third (Runnable printThird ) throws InterruptedException {
98
+ s3. acquire();
99
+ printThird. run();
100
+ }
101
+ }
102
+ ```
60
103
104
+ ### ** C++**
105
+
106
+ ``` cpp
107
+ class Foo {
108
+ private:
109
+ mutex m2, m3;
110
+
111
+ public:
112
+ Foo() {
113
+ m2.lock();
114
+ m3.lock();
115
+ }
116
+
117
+ void first(function<void()> printFirst) {
118
+ printFirst();
119
+ m2.unlock();
120
+ }
121
+
122
+ void second (function<void()> printSecond) {
123
+ m2.lock();
124
+ printSecond();
125
+ m3.unlock();
126
+ }
127
+
128
+ void third(function<void()> printThird) {
129
+ m3.lock();
130
+ printThird();
131
+ }
132
+ };
61
133
```
62
134
63
135
### **...**
Original file line number Diff line number Diff line change 1
1
class Foo {
2
+ private:
3
+ mutex m2, m3;
4
+
2
5
public:
3
6
Foo () {
4
- _mutex1 .lock ();
5
- _mutex2 .lock ();
7
+ m2 .lock ();
8
+ m3 .lock ();
6
9
}
7
10
8
11
void first (function<void ()> printFirst) {
9
-
10
12
printFirst ();
11
- _mutex1 .unlock ();
13
+ m2 .unlock ();
12
14
}
13
15
14
16
void second (function<void ()> printSecond) {
15
-
16
- _mutex1.lock ();
17
+ m2.lock ();
17
18
printSecond ();
18
- _mutex1.unlock ();
19
- _mutex2.unlock ();
19
+ m3.unlock ();
20
20
}
21
21
22
22
void third (function<void ()> printThird) {
23
-
24
- _mutex2.lock ();
23
+ m3.lock ();
25
24
printThird ();
26
- _mutex2.unlock ();
27
25
}
28
-
29
-
30
- private:
31
- std::mutex _mutex1;
32
- std::mutex _mutex2;
33
-
34
26
};
Original file line number Diff line number Diff line change 1
- import java .util .concurrent .Semaphore ;
2
-
3
1
class Foo {
4
- private Semaphore twoS = new Semaphore (0 );
5
- private Semaphore threeS = new Semaphore (0 );
6
-
2
+
3
+ private final Semaphore s2 = new Semaphore (0 );
4
+ private final Semaphore s3 = new Semaphore (0 );
5
+
7
6
public Foo () {
8
-
9
7
}
10
8
11
9
public void first (Runnable printFirst ) throws InterruptedException {
12
10
printFirst .run ();
13
- twoS .release ();
11
+ s2 .release ();
14
12
}
15
13
16
14
public void second (Runnable printSecond ) throws InterruptedException {
17
- twoS .acquire ();
15
+ s2 .acquire ();
18
16
printSecond .run ();
19
- threeS .release ();
17
+ s3 .release ();
20
18
}
21
19
22
20
public void third (Runnable printThird ) throws InterruptedException {
23
- threeS .acquire ();
21
+ s3 .acquire ();
24
22
printThird .run ();
25
23
}
26
- }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Foo :
2
+ def __init__ (self ):
3
+ self .l2 = threading .Lock ()
4
+ self .l3 = threading .Lock ()
5
+ self .l2 .acquire ()
6
+ self .l3 .acquire ()
7
+
8
+ def first (self , printFirst : 'Callable[[], None]' ) -> None :
9
+ printFirst ()
10
+ self .l2 .release ()
11
+
12
+ def second (self , printSecond : 'Callable[[], None]' ) -> None :
13
+ self .l2 .acquire ()
14
+ printSecond ()
15
+ self .l3 .release ()
16
+
17
+ def third (self , printThird : 'Callable[[], None]' ) -> None :
18
+ self .l3 .acquire ()
19
+ printThird ()
Original file line number Diff line number Diff line change @@ -63,22 +63,97 @@ class FooBar {
63
63
64
64
<!-- 这里可写通用的实现逻辑 -->
65
65
66
+ 两把锁分别对应 ` foo ` 和 ` bar ` ,先把 ` bar ` 锁住,确保第一个输出的是 ` foo `
67
+
66
68
<!-- tabs:start -->
67
69
68
70
### ** Python3**
69
71
70
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
73
72
74
``` python
73
-
75
+ class FooBar :
76
+ def __init__ (self , n ):
77
+ self .n = n
78
+ self .fooLock = threading.Lock()
79
+ self .barLock = threading.Lock()
80
+ self .barLock.acquire()
81
+
82
+ def foo (self , printFoo : ' Callable[[], None]' ) -> None :
83
+ for i in range (self .n):
84
+ self .fooLock.acquire()
85
+ printFoo()
86
+ self .barLock.release()
87
+
88
+ def bar (self , printBar : ' Callable[[], None]' ) -> None :
89
+ for i in range (self .n):
90
+ self .barLock.acquire()
91
+ printBar()
92
+ self .fooLock.release()
74
93
```
75
94
76
95
### ** Java**
77
96
78
97
<!-- 这里可写当前语言的特殊实现逻辑 -->
79
98
80
99
``` java
100
+ class FooBar {
101
+ private int n;
102
+ private final Semaphore fooSem = new Semaphore (1 );
103
+ private final Semaphore barSem = new Semaphore (0 );
104
+
105
+ public FooBar (int n ) {
106
+ this . n = n;
107
+ }
108
+
109
+ public void foo (Runnable printFoo ) throws InterruptedException {
110
+ for (int i = 0 ; i < n; i++ ) {
111
+ fooSem. acquire();
112
+ printFoo. run();
113
+ barSem. release();
114
+ }
115
+ }
116
+
117
+ public void bar (Runnable printBar ) throws InterruptedException {
118
+ for (int i = 0 ; i < n; i++ ) {
119
+ barSem. acquire();
120
+ printBar. run();
121
+ fooSem. release();
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ ### ** C++**
81
128
129
+ ``` cpp
130
+ class FooBar {
131
+ private:
132
+ int n;
133
+ mutex fooMu, barMu;
134
+
135
+ public:
136
+ FooBar(int n) {
137
+ this->n = n;
138
+ barMu.lock();
139
+ }
140
+
141
+ void foo(function<void()> printFoo) {
142
+ for (int i = 0; i < n; i++) {
143
+ fooMu.lock();
144
+ printFoo();
145
+ barMu.unlock();
146
+ }
147
+ }
148
+
149
+ void bar (function<void()> printBar) {
150
+ for (int i = 0; i < n; i++) {
151
+ barMu.lock();
152
+ printBar();
153
+ fooMu.unlock();
154
+ }
155
+ }
156
+ };
82
157
```
83
158
84
159
### **...**
You can’t perform that action at this time.
0 commit comments