class ThreadDemo5
{
public static void main(String[] args)
{
//test t=new test();
//t.run();
TestThread t=new TestThread();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
Thread t3 = new Thread(t);
Thread t4 = new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TestThread implements Runnable
{
private int tickets=100;
public void run()
{
while(true)
{
if(tickets>0)
{
try //用try代码块试验sleep方法,观察线程切换的情况
{
Thread.sleep(10);//这个方法本身已经通过Throws关键词申明可能会出错,所以用try处理
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println(Thread.currentThread().getName()+"正在出售第"+tickets--);
}
}
}
结果程序运行后,发现if语句无效了:
帮忙看下哪里的问题 |
|