题目:
编写一个多线程程序,模拟火车售票窗口的售票功能。创建线程一盒线程二,通过这两个线程共同售出100张票。
public class Text {
public static void main(String[] args) {
TicketWindow tw = new TicketWindow();
new Thread(tw,"线程一").start();
new Thread(tw,"线程二").start();
}
}
class TicketWindow implements Runnable{
private int num = 1;
public void run(){
while (num < 101){
Thread th = Thread.currentThread();
String th_name = th.getName();
System.out.println(th_name + "正在发售第" + num++ + "张票");
}
}
}
运行结果