Skip to content

Commit 1e228eb

Browse files
committed
lock test
1 parent 1defc1a commit 1e228eb

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package acmr.javacore.basic.thread;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* @author guoguo
8+
* @version 1.0.0
9+
* @date 2022/6/26
10+
* @description
11+
*/
12+
public class ReentrantLockTest2 {
13+
public static ReentrantLock lock = new ReentrantLock();
14+
public static int count = 0;
15+
16+
public static void main(String[] args) throws InterruptedException {
17+
Thread thread1 = new Thread(() -> {
18+
for(int i = 0; i < 10000; i++) {
19+
lock.lock();
20+
try {
21+
System.out.println(Thread.currentThread().getName() + " 竞争上岗");
22+
count++;
23+
} finally {
24+
lock.unlock();
25+
}
26+
}
27+
});
28+
Thread thread2 = new Thread(() -> {
29+
for(int i = 0; i < 10000; i++) {
30+
lock.lock();
31+
try {
32+
System.out.println(Thread.currentThread().getName() + " 竞争上岗");
33+
count++;
34+
} finally {
35+
lock.unlock();
36+
}
37+
}
38+
});
39+
40+
thread1.start();
41+
thread2.start();
42+
thread1.join();
43+
thread2.join();
44+
45+
System.out.println(count);
46+
}
47+
}

0 commit comments

Comments
 (0)