需求:多个窗口同时开始买票
方案一:
class Ticket extends Thread {
private static int num = 100;
Ticket(String name) {
super(name);
}
public void run() {
while(num>0) {
System.out.println(Thread.currentThread() + " --" + num--);
}
}
}
class Demo {
public static void main(String[] args) {
Ticket t1 = new Ticket("线程一");
Ticket t2 = new Ticket("线程二");
Ticket t3 = new Ticket("线程三");
t1.start();
t2.start();
t3.start();
}
}
这样写是没问题的,但是num是静态的,比较浪费内存,哪有没有更好的方案了:
方案二:
class Ticket implements Runnable {
private int num = 100;
public void run() {
while(num>0) {
System.out.println(Thread.currentThread()+"--" + num--);
}
}
}
class Demo2 {
public