package com.entity;
public class Ticket implements Runnable{
private int num=200;
@Override
public void run() {
for (int i = 0; i <= num; i++) {
if(this.num>0){
//返回对当前正在执行的线程对象的引用。
System.out.println(Thread.currentThread().getName()+" 卖票,当前卖出的票号为:"+num--);
}
}
}
}
package com.test;
import com.entity.Ticket;
//2. 三个窗口同时卖票 用 实现Runnable方式
public class Test {
public static void main(String[] args) {
Ticket t= new Ticket();
Thread a= new Thread(t);
a.setName("1号售票站");
a.start();
Thread b= new Thread(t);
b.setName("2号售票站");
b.start();
Thread c= new Thread(t);
c.setName("3号售票站");
c.start();
}
}
2. 三个窗口同时卖票 用 实现Runnable方式package com.entity;public class Ticket implements Runnable{ private int num=200; @Override public void run() { for (int i = 0; i <= num; i++) { if(this.num>0){ //返回对当前正