同步代码块

class  Test
{
public  static  void  main(String[]  args)
{
TestThread tt  =new  TestThread();
for(int i=0;i<5;i++)
{
new Thread(tt).start();
}

}
class   TestThread   implements  Runnable
{
int  tickets  =100;
String   str =new   String("");
public  void   run()
{
while(true)
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{}
System.out.println(Thread.currentThread().getName()+"is  sailing  ticket:"+tickets--);
}
}
}
}
}
}

posted on 2012-03-02 08:56  平安夜  阅读(116)  评论(0编辑  收藏  举报