class ThreadDemo2
{
public static void main(String []args)
{
TestThread2 tt=new TestThread2();
new Thread(tt).start(); //四个线程同时卖同100张票
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}class TestThread2 implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
while(true)
{
syncronized(str)
{
if(tickets>0)
{
try{Thread.sleep(10);}catch(Exception e){ e.getMessage();}
System.out.println(Thread.currentThread().getName()+" is selling tickets "+tickets--);
}
}
}
}
}
编译后不能通过,提示syncronized下面的“{”前缺少分号,去掉syncronized后程序是正常运行的啊,怎么回事呢?
{
public static void main(String []args)
{
TestThread2 tt=new TestThread2();
new Thread(tt).start(); //四个线程同时卖同100张票
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}class TestThread2 implements Runnable
{
int tickets=100;
String str=new String("");
public void run()
{
while(true)
{
syncronized(str)
{
if(tickets>0)
{
try{Thread.sleep(10);}catch(Exception e){ e.getMessage();}
System.out.println(Thread.currentThread().getName()+" is selling tickets "+tickets--);
}
}
}
}
}
编译后不能通过,提示syncronized下面的“{”前缺少分号,去掉syncronized后程序是正常运行的啊,怎么回事呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货