class Ticket implements Runnable
{
private int tic = 100;
//Object o = new Object(); 看这里看这里 public void run()
{
while (true)
{
synchronized(new Object())//改成synchronized(o),这两个有什么区别?运行结果是不一样的 {
if (tic > 0)
{
try
{
Thread.sleep(10); }
catch (Exception e)
{
//以后再学
}
System.out.println(Thread.currentThread().getName()+" sell ticket "+tic--);
}
}
}
}
}
class TicketDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
} |
|