-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaleTicket.java
More file actions
71 lines (56 loc) · 1.7 KB
/
Copy pathSaleTicket.java
File metadata and controls
71 lines (56 loc) · 1.7 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
65
66
67
68
69
70
71
package sync;
/**
* @author : CodeWater
* @create :2022-06-02-16:14
* @Function Description :多线程----卖票
* 线程创建的集中方式:
* 1. 继承thread类
* 2. 实现runnable接口
* 3. 使用callable接口
* 4. 使用线程池
*/
//第一步 创建资源类,定义属性和和操作方法
class Ticket{
//票数
private int number = 30;
//操作方法:卖票. synchronized自动上锁和释放锁
public synchronized void sale(){
//判断:是否有票
if( number > 0 ){
System.out.println( Thread.currentThread().getName() + ":卖出" + (number--) + ", 还剩:" + number );
}
}
}
public class SaleTicket {
//第二步 创建多个线程,调用资源类的操作方法
public static void main( String[] args ) {
//创建Ticket对象
Ticket ticket = new Ticket();
//创建三个线程
new Thread(new Runnable() {
@Override
public void run() {
//调用卖票方法
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale();
}
}
} , "AA" ).start();
new Thread( new Runnable() {
@Override
public void run(){
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale();
}
}
} , "BB" ).start();
new Thread( new Runnable() {
@Override
public void run(){
for( int i = 0 ; i < 40 ; i++ ){
ticket.sale();
}
}
} , "CC" ).start();
}
}