File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ package chapter3 ;
2+
3+ import java .util .concurrent .locks .ReentrantLock ;
4+
5+ /**
6+ * ÖØÈëËø
7+ * Created by 13 on 2017/5/5.
8+ */
9+ public class ReenterLock implements Runnable {
10+ public static ReentrantLock lock = new ReentrantLock ();
11+ public static int i = 0 ;
12+
13+ @ Override
14+ public void run () {
15+ for (int j = 0 ; j < 1000000 ; j ++) {
16+ lock .lock ();
17+ try {
18+ i ++;
19+ } finally {
20+ lock .unlock ();
21+ }
22+
23+ }
24+ }
25+
26+ public static void main (String args []) throws InterruptedException {
27+ ReenterLock reenterLock = new ReenterLock ();
28+ Thread thread1 = new Thread (reenterLock );
29+ Thread thread2 = new Thread (reenterLock );
30+
31+ thread1 .start ();
32+ thread2 .start ();
33+
34+ thread1 .join ();
35+ thread2 .join ();
36+
37+ System .out .println (i );
38+ }
39+
40+ }
You can’t perform that action at this time.
0 commit comments