-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockTest1.java
More file actions
59 lines (45 loc) · 1.34 KB
/
LockTest1.java
File metadata and controls
59 lines (45 loc) · 1.34 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
54
55
56
57
58
59
package Thread;
//解决线程安全问题方式三:Lock
import java.util.concurrent.locks.ReentrantLock;
class WindowLock implements Runnable{
private int ticket = 100;
//1.实例化ReentrantLock
private ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while(true){
try{
//2.调用锁定方法lock()
lock.lock();
if(ticket > 0){
try {
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":卖票,票号为:" + ticket);
ticket--;
}else {
break;
}
}finally {
//3.调用解锁方法unlock()
lock.unlock();
}
}
}
}
public class LockTest1 {
public static void main(String[] args) {
WindowLock wl = new WindowLock();
Thread t1 =new Thread(wl);
Thread t2 =new Thread(wl);
Thread t3 =new Thread(wl);
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}