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的使用率就100%,看了很久也没有找出问题 能谁能解答一下???
该贴已经同步到 杨玉揆的微博 |
|