Question about Java synchronized

后端 未结 7 1551
小蘑菇
小蘑菇 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:22

    Not unless the method is static. A synchronized non-static method takes a lock on the object (instance) on which it is invoked, not on the class.

    A synchronized static method takes a lock on the class, so that could help - but it's often not very practical.

    What you could do is have a static member object in your class, and do a synchronized block on that (class-global) object in your process method.

    0 讨论(0)
  • 2020-12-01 07:23

    No, synchronized will not do this. More specifically synchronized on the instance level will not do this. Instead you will have to synchronize on the class level.

    For example instead of having:

    public synchronized method()
    {
        //do stuff
    }
    

    You will have to code as:

    public void method()
    {
        synchronized(this.getClass())
        {
            //do stuff
        }
    }
    
    0 讨论(0)
  • 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
    
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:31

    No, the method synchronized locks on the specific object (the 'this'), so 2 instances of the same class would lock on different objects.

    If you want to synchronize across all instances of a class, you need to use a synchronized block IN the method, locking on a static final object.

    0 讨论(0)
  • 2020-12-01 07:33

    I slightly disagree with Aasmund -- though agree a bit at the same time: If you use a construct like this:

    class Worker {
    ...
     public synchronized void process() {...}
    }
    

    then Aasmund is right -- that won't prevent multiple instances of the Worker class to execute process() in parallel. However, you can use the synchronized keyword still to synchronize on a static member to prevent that from happening:

    class Worker {
     static Object o = new Object();
     ...
     public void process() {
      synchronized(o) {
       ...//perform the work here
      }
     }
    
    0 讨论(0)
  • Only one thread can hold a lock on an object at any one time.

    A synchronized method attempt to hold a lock on the instance. Another thread can also hold a lock on another instance of the same class. Another thread cannot enter another synchronized method of the same instance. i.e. it is not the method which is locked.

    However a thread can enter a non-synchronized method while another thread holds a lock on that object. Only synchronized methods are protected.

    A static synchronized method obtains a lock on the Class rather than the object. However, the follows the same rules as non-static method except with a different object.

    Note: There is no "instance" for static method even though you can write code which appears to use an instance. e.g. instance.staticMethod()

    0 讨论(0)
提交回复
热议问题