多线程火车票实例
继承Thread类
public class Myticket{
public static void main(String[] args){
new MyThread("窗口1:").start();
new MyThread("窗口2:").start();
}
}
public class MyThread{
public MyThread(){
super();
}
public MyThread(String str){
super(str);
}
staitc int ticket = 100;
@Ooverride
public void run(){
while(true){
if(ticket <= 0){
break;
}else{
System.out.println(getName()+"卖出了第:"+(ticket--)+"号票");
}
}
}
}
继承Runnable接口
public class Myticket{
public static void main(String[] args){
MyRunnable mr = new MyRunnable();
new Thread(mr,"窗口1:").start();
new Thread(mr,"敞口2:").start();
}
}
public class MyRunnable implements Runnable{
int ticket = 100;
@Override
public void run(){
while(true({
if(ticket <= 0){
break;
}else{
System.in.println(Thread.currentThread().getName()+"卖出了第:"+(ticket--)+"号票");
}
}
}
}