synchronized block not locking the object reference

前端 未结 3 2048
梦谈多话
梦谈多话 2021-01-24 01:51
class Demo
{
    void demo()
    {
        System.out.println(\"Inside demo of \"+Thread.currentThread().getName());
        try
        {
            Thread.sleep(10000         


        
3条回答
  •  感情败类
    2021-01-24 02:33

    The synchronization only occurs in Thread1. Since Thread2 does not synchronize on d, it is allowed to invoke demo() while Thread1 holds the lock.

    You seem to be misunderstanding the use of synchronized. Synchronization only occurs with other threads trying to enter a synchronized block of the common object.

提交回复
热议问题