Question about Java synchronized

后端 未结 7 1563
小蘑菇
小蘑菇 2020-12-01 06:53

The Java documentation says that \"it is not possible for two invocations of synchronized methods on the same object to interleave\". What I need to know is whether synchro

7条回答
  •  借酒劲吻你
    2020-12-01 07:25

    If you make the method process static, it would only allow one invocation of the method at the same time.

    If that is not possible, have a static variable, say Integer lock; And use synchronized (lock) inside your method process. that is

    process()
    {
        synchronized (lock)
        {
    
            // all your code
    
        }
    }
    

提交回复
热议问题