-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockTest.java
More file actions
53 lines (44 loc) · 1.37 KB
/
Copy pathLockTest.java
File metadata and controls
53 lines (44 loc) · 1.37 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ThreadTest.others;
import java.util.concurrent.locks.ReentrantLock;
/* ReentrantLock is another way just like synchronized()
二者都能解决线程安全问题
* 不同: it will unclock && lock mannualy (手动加锁解锁), not automatically like synchronized
*
* synchronized will auto release the lock when the synchronise code finished executed
*
* 优先使用顺序: Lock --> 同步代码块 (已经进入方法体, 分配了相应资源) --> 同步方法
* */
class Window implements Runnable {
private int tickets = 100;
ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + " sold :" + tickets);
tickets--;
} else {
break;
}
} finally {
lock.unlock();
}
}
}
}
public class LockTest {
public static void main(String[] args) {
Window window = new Window();
Thread t1 = new Thread(window);
Thread t2 = new Thread(window);
Thread t3 = new Thread(window);
t1.setName("线程A");
t2.setName("线程B");
t3.setName("线程C");
t1.start();
t2.start();
t3.start();
}
}