标签:art 抢票 web im lse test throws 使用 run pre
抢票线程使用同步块修改public class n {
public static void main(String[]args) throws InterruptedException
{
web wb=new web();
new Thread(wb,"a").start();
new Thread(wb,"b").start();
new Thread(wb,"c").start();
}
}
class web implements Runnable{
int num=10;
private boolean flag=true;
public void run()
{
while(flag)
{
test2();
}
}
//修改同步块
public void test2()
{
synchronized(this)
{
if(num<0)
{
flag=false;
return;
}
try {
Thread.sleep(200);
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->"+num--);
}
}
public synchronized void test()
{ if(num<0)
{
flag=false;
return;
}
try {
Thread.sleep(200);
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->"+num--);
}
}
标签:art 抢票 web im lse test throws 使用 run pre
原文地址:https://blog.51cto.com/14437184/2429304