日期:2014-05-18 浏览次数:20648 次
public class ThreadDemo2 {
public static void main(String[] args) {
Ticket t1 = new Ticket();
Ticket t2 = new Ticket();
Ticket t3 = new Ticket();
Ticket t4 = new Ticket();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Ticket extends Thread {
private int ticket;
Object obj = new Object();
public void run() {
while(true) {
synchronized (obj) {
if (ticket < 10) {
System.out.println(Thread.currentThread().getName()
+ "--->" + ticket);
ticket++;
}
}
}
}
}