Skip to content

Commit b50b13a

Browse files
committed
ReenterLock
1 parent 28d39fa commit b50b13a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/chapter3/ReenterLock.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)