线程:实现Runnable车站卖票,但是代码还不成熟

来源:互联网 发布:淘宝复制别人店铺模板 编辑:程序博客网 时间:2024/04/27 23:36

车站卖票,

class Ticket02 implements Runnable{
private int ticket = 10;

//卖票操作
public void run() {

for(int i=0;i<100;i++){
if(this.ticket>0){
ticket--;
System.out.println((Thread.currentThread().getName())+"窗口,卖出的第:"+ticket+"张票");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}
public class TicketDemo {
public static void main(String[] args) {
Ticket02 t1 = new Ticket02();//共享资源

Thread th1 = new Thread(t1);//开启线程,给线程重新命名

th1.start();

new Thread(t1,"线程A").start();//把每个售票窗口看作一个线程
new Thread(t1,"线程B").start();
new Thread(t1,"线程C").start();
new Thread(t1,"线程D").start();
}
}
0 0
原创粉丝点击