多线程买火车票

public class TestWindowsPiao {
public static void main(String[] args) throws InterruptedException {
Windows1 w = new Windows1();
Thread t = new Thread(w);
Thread t1 = new Thread(w);
Thread t2 = new Thread(w);
t.setName("线程一");
t1.setName("线程二");
t2.setName("线程三");
t.start();
t1.start();
t2.start();
}
}


class Windows1 implements Runnable {
int ticket = 100;


public void run() {
while (true) {
synchronized (this) {
if (ticket > 0) {
System.out.println(Thread.currentThread().getName()+"票号:"+ticket--);
}
}
}
}
}

你可能感兴趣的:(多线程买火车票)