File tree Expand file tree Collapse file tree
javacore/src/main/java/acmr/javacore/basic/thread Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments