-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLock.java
More file actions
30 lines (26 loc) · 808 Bytes
/
Copy pathMyLock.java
File metadata and controls
30 lines (26 loc) · 808 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
package ThreadTest2;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyLock implements Runnable {
int poll = 100;
Lock lock = new ReentrantLock();//声明lock锁。
//LOCK 接口
//Reentrantlock类
@Override
public void run() {
while (true) {
// synchronized (MyLock.class) {
lock.lock();//加锁
if (poll > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在售出第" + (poll--) + " 张票");
}
lock.unlock();//释放锁
// }
}
}
}