-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSaleTicket.java
More file actions
64 lines (52 loc) · 1.65 KB
/
Copy pathLSaleTicket.java
File metadata and controls
64 lines (52 loc) · 1.65 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
60
61
62
63
64
package lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author : CodeWater
* @create :2022-06-02-16:40
* @Function Description :使用lock接口来实现卖票
* 1. lock是一个接口, 手动上和释放。 发生异常可能会死锁
* 2. synchronized是一个关键字,自动上锁和释放
*/
//第一步 创建资源类,定义属性和和操作方法
class LTicket {
//票数量
private int number = 30;
//创建可重入锁 lock的一个实现类
private final ReentrantLock lock = new ReentrantLock();
//卖票方法
public void sale(){
//上锁
lock.lock();
try{
//判断是否有票
if( number > 0 ){
System.out.println( Thread.currentThread().getName() + "卖出了:" + (number--) + ",还剩:" + number );
}
}finally{
//解锁
lock.unlock();
}
}
}
public class LSaleTicket {
//第二步 创建多个线程,调用资源类的操作方法
//创建三个线程
public static void main(String[] args){
LTicket ticket = new LTicket();
new Thread( () -> {
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale();
}
} , "AA").start(); //线程的创建是不确定的,因为他内部用的native
new Thread( () -> {
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale();
}
} , "BB" ).start();
new Thread( () -> {
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale() ;
}
} , "CC" ).start();
}
}