class sale implements Runnable
{
private int t =100;
Object o = new Object();
public void run ()
{
synchronized(o)
{
while (t>0)
{
try
{
Thread.sleep(10);
}
catch ( InterruptedException i)
{
}
System.out.println(Thread.currentThread().getName()+"....."+t--);
}
}}
}
class TicketDemo
{
public static void main(String[]args)
{
sale s = new sale();
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
Thread t3 = new Thread(s);
Thread t4 = new Thread(s);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
-------------------------------------------
添加对象时为什么需要Object o = new Object();再synchronized(o)
而不能直接synchronized(new Object())?
|
|