实现方式一:继承Thread线程类
public class TestThread2 extends Thread {
//模拟多个窗口买票的场景
private static int num=300;
public void run(){
while(num>0){
//线程安全问题
System.out.println(this.getName()+"卖出了第"+num+"号票!");
num--;
}
}
public static void main(String[] args) {
TestThread2 tt1=new TestThread2();
tt1.setName("窗口一:");
tt1.start();
TestThread2 tt2=new TestThread2();
tt2.setName("窗口二:");
tt2.start();
TestThread2 tt3=new TestThread2();
tt3.setName("窗口三:");
tt3.start();
}
}
方式二:实现runnable接口
public class TestRunble2 implements Runnable{
//模拟多个窗口买票的场景
/*
* 实现Runable接口创建线程的好处:<