-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketThread.java
More file actions
80 lines (56 loc) · 1.29 KB
/
TicketThread.java
File metadata and controls
80 lines (56 loc) · 1.29 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
72
73
74
75
/*
* 功能:测试多线程,售票,线程安全问题,通过synchronized (Object)对象锁实现线程安全同步
*/
package com.bj.thread;
/*
* 功能:模拟多个窗口取票,验证取票是否会出现线程安全问题
*/
import javax.swing.plaf.synth.SynthSpinnerUI;
public class TicketThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义三个窗口
TicketWindow tw1=new TicketWindow();
//TicketWindow tw2=new TicketWindow();
//TicketWindow tw3=new TicketWindow();
Thread t1=new Thread(tw1);
Thread t2=new Thread(tw1);
Thread t3=new Thread(tw1);
t1.start();
t2.start();
t3.start();
}
}
//售票窗口
class TicketWindow implements Runnable{
//票数
private int nums=2000;
//private DogI di=new DogI();
public void run(){
while(true){
//出票速度,1s一张
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//认为if else 要保证其原子性,加对象锁,保证锁内同步代码进行
synchronized (this) {
//先判断是否还有票
if(nums>0){
System.out.println(Thread.currentThread().getName()+"在售出第"+(nums)+"票");
nums--;
}else{
//售票结束
break;
}
}
}
}
}
class DogI{
public DogI(){
System.out.println("Dog");
}
}