弄了两小时了,好急躁,麻烦大神帮我看下! 耽搁你几分钟时间帮我一下,谢谢了!
我不加synchronized运行的时候就是多线程,加了为什么就变成单线程了,怎么运行都是一个线程在走。
class RunInfo implements Runnable
{
private int tick = 100;
public void run()
{
while(true)
{
show();
}
}
public synchronized void show()
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"::"+tick--);
}
}
}
class SynchronizedDemo
{
public static void main(String[] args)
{
RunInfo rf= new RunInfo();
Thread t1 = new Thread(rf);
Thread t2 = new Thread(rf);
Thread t3 = new Thread(rf);
Thread t4 = new Thread(rf);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
|
|