java-为什么synchronized块没有给出正确的循环结果

pra*_*mar 1 java multithreading

我写了一个代码,创建了几个线程并启动它.我使用synchronized block锁定了对象的监视器.我希望创建的第一个线程应该锁定对象并完成其工作.然后任何其他对象都可以输入它.

但它没有发生,程序在下面.

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();
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望结果应该是这样的.

首先,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
Run Code Online (Sandbox Code Playgroud)

Lok*_*esh 6

问题是你每次都在创建新对象: new ThreadCreationDemo()

因此所有线程都获得对不同对象的锁定,因此锁定将失败.