本帖最后由 赵庆礼 于 2012-6-27 11:47 编辑
class Ticket implements Runnable
{
private int tick =100;
private Object obj = new Object();
public void run()
{
while (true)
{
synchronized(obj)
{
if(tick>0)
{
try
{
Thread.sleep(10);
System.out.println(Thread.currentThread().getName()+"run ...."+tick--);
}
catch (Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"run ...."+tick--);
}
}
}
}
}
class ThreadDemo2
{
public static void main(String[] args)
{
Ticket t = new Ticket();
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();
}
}
这儿程序执行后为什么cpu占用率特别大,调试了很久没有找到原因在哪 |