Skip to content

Commit 442617b

Browse files
author
fengyuan.wan
committed
lock
1 parent 5edff68 commit 442617b

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package java0.conc0302.threadpool;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @Desc
8+
* @Author wfy
9+
* @Date 2021/2/3 18:05
10+
*/
11+
public class ReentrantLockTest {
12+
static Lock lock = new ReentrantLock(true);
13+
static int count = 0;
14+
15+
public static void main(String[] args) {
16+
for (int i = 0; i < 10; i++) {
17+
new Thread(ReentrantLockTest::incr).start();
18+
}
19+
20+
}
21+
22+
public static void incr() {
23+
lock.lock();
24+
try {
25+
for (int i = 0; i < 10000; i++) {
26+
count++;
27+
System.out.println(count);
28+
}
29+
} finally {
30+
lock.unlock();
31+
}
32+
33+
}
34+
}

0 commit comments

Comments
 (0)