Skip to content

Commit 01df6a9

Browse files
committed
ReentrantLockExample
1 parent 3171394 commit 01df6a9

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package chapter03;
2+
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
class ReentrantLockExample {
6+
7+
int a = 0;
8+
ReentrantLock lock = new ReentrantLock();
9+
10+
public void writer() {
11+
12+
lock.lock(); // »ñÈ¡Ëø
13+
14+
try {
15+
a++;
16+
} finally {
17+
lock.unlock(); // ÊÍ·ÅËø
18+
}
19+
}
20+
21+
public void reader() {
22+
23+
lock.lock(); // »ñÈ¡Ëø
24+
25+
try {
26+
int i = a;
27+
// ¡­¡­
28+
} finally {
29+
lock.unlock(); // ÊÍ·ÅËø
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)