-
Notifications
You must be signed in to change notification settings - Fork 0
/
T3.java
82 lines (68 loc) · 1.63 KB
/
T3.java
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
76
77
78
79
80
81
82
/**
*
*/
package forbasic;
/**
* @author Administrator
* @title 购票 高并发资源共享思路 实现Runnable接口就可以实现资源共享
* @Date 2018年6月28日
*/
/*
*
public class T3 extends Thread{
/*
* Thread线程时间没有实现资源共享
private int num=5;
public void run(){
for(int i=0;i<10;i++){
if(this.num>0){
System.out.println(Thread.currentThread().getName()+"买票"+this.num--);
}
}
}
public static void main(String args[]){
//定义三个线程 并开启
T3 t1=new T3();
t1.setName("窗口1");
T3 t2=new T3();
t2.setName("窗口2");
T3 t3=new T3();
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
*/
public class T3 implements Runnable{
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
private int num=5;
@Override
public void run() {
// TODO Auto-generated method stub
/*for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"买票"+this.num--);
if(num==0){
System.out.println("----------");
//break;
}
}*/
do{
System.out.println(Thread.currentThread().getName()+"买票"+this.num--);
}while(num>0);
}
public static void main(String args[]){
T3 t=new T3();
Thread th1=new Thread(t);
th1.setName("售票口一");
Thread th2 = new Thread(t); //线程二
th2.setName("售票口二");
Thread th3 = new Thread(t); //线程三
th3.setName("售票口三");
th1.start();
th2.start();
th3.start();
}
}