-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathReenterLock.java
More file actions
32 lines (28 loc) · 778 Bytes
/
Copy pathReenterLock.java
File metadata and controls
32 lines (28 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ch3.s1;
import java.util.concurrent.locks.ReentrantLock;
public class ReenterLock implements Runnable {
public static ReentrantLock lock = new ReentrantLock();
public static int i=0;
public void run() {
for(int j=0;j<10000000;j++){
lock.lock();
// lock.lock();
try {
i++;
}finally {
lock.unlock();
// lock.unlock();
}
}
}
public static void main(String[] args) throws InterruptedException {
ReenterLock r1 = new ReenterLock();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r1);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(i);
}
}