同步函数
class ThreadDemo1
{
public static void main(String[] args)
{
TestThread tt=new TestThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TestThread implements Runnable
{
String str=new String("");
int tickets=100;
public void run()
{
while(true)
{
sale();
}
}
public synchronized void sale()
{
if(tickets>0)
{
try{
Thread.sleep(100);
}catch(Exception e)
{}
System.out.println(Thread.currentThread().getName()+"ticket is:"+tickets--);
}
}
}