我编写了一个创建多个线程并启动它的代码。我使用同步块锁定了对象上的监视器。我希望创建的第一个线程应该锁定对象并完成其工作。然后任何其他对象都可以进入它。
但它没有发生,程序在下面。
class ThreadCreationDemo implements Runnable
{
public void run()
{
synchronized(this)
{
for(int i=0;i<10;i++)
{
System.out.println("i: "+i+" thread: "+Thread.currentThread().getName()+" threadgroup: "+Thread.currentThread().getThreadGroup()+" "+Thread.holdsLock(this));
try {
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
}
public static void main(String args[])
{
Thread t[]=new Thread[5];
for(int i=0;i<5;i++)
{
t[i]=new Thread(new ThreadCreationDemo());
t[i].start();
}
}
}
我希望结果应该是这样的。
首先,i=0 到 9 的所有值都打印在线程名称下,例如线程 0,然后是线程 1,依此类推。
但输出是这样的:
i: 0 thread: Thread-1
i: 0 thread: Thread-3
i: 0 thread: Thread-2
i: 0 thread: Thread-0
i: 0 thread: Thread-4
i: 1 thread: Thread-1
i: 1 thread: Thread-4
i: 1 thread: Thread-3
i: 1 thread: Thread-0
i: 1 thread: Thread-2
i: 2 thread: Thread-1
i: 2 thread: Thread-3
i: 2 thread: Thread-2
i: 2 thread: Thread-0
i: 2 thread: Thread-4
i: 3 thread: Thread-1
i: 3 thread: Thread-3
i: 3 thread: Thread-0
i: 3 thread: Thread-4
i: 3 thread: Thread-2
i: 4 thread: Thread-1
i: 4 thread: Thread-3
i: 4 thread: Thread-2
i: 4 thread: Thread-4
i: 4 thread: Thread-0
i: 5 thread: Thread-1
i: 5 thread: Thread-4
i: 5 thread: Thread-3