public class ThreadTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
WindowTicket wt = new WindowTicket();
Thread t1 = new Thread(wt);
Thread t2 = new Thread(wt);
Thread t3 = new Thread(wt);
t1.start();
t2.start();
t3.start();
}
}
class WindowTicket implements Runnable
{
int ticket = 1000;
Object obj = new Object();
public void run()
{
synchronized(obj)
{
while(ticket>0)//因为你这儿有个while判断,.只要ticket>0就一直循环里面的内容, 上面又加了锁,所以一个线程进来之后
//其他线程都进不来就执行完了 {
//Thread.sleep(10);
System.out.println(Thread.currentThread().getName()+"---"+ticket--);
}
}
}
}
|