public class Test implements Runnable{
private int tickets = 10;
public void run() {
while(true) {
synchronized(this) {
if(tickets>0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"票的号码:"+tickets--);
} else {
break;
}
}
}
}
public static void main(String[] args) {
Test r = new Test();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);
Thread t4 = new Thread(r);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
你的基础上改了。。只是将Object 那对象改成this。你试着运行一下。。估计你和看的视频有出入哦。