Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method</p> </blockquote> <p>Yes and no.</p> <p>If you are in a <code>synchronized</code> method, then calls to other methods that are also <code>synchronized</code> by other threads are locked. However calls to non-synchronized methods by other threads are <em>not</em> locked -- anyone can call them at the same time.</p> <pre><code>public synchronized void someSynchronizedMethod() { ... someNonSynchronizedMethod(); ... } // anyone can call this method even if the someSynchronizedMethod() method has // been called and the lock has been locked public void someNonSynchronizedMethod() { ... } </code></pre> <p>Also, if you call <code>someSynchronizedMethod()</code> but happen to be within the <code>someNonSynchronizedMethod()</code> method, you still hold the lock. The lock is enabled when you enter a synchronized block and is disabled when you exit that method. You can call all sorts of other unsynchronized methods and they will still be locked.</p> <p>But you are asking two different things in your question:</p> <blockquote> <p>In Java, if a synchronized method contains a call to a non-synchronized, can another method still access the non-synchronized method at the same time?</p> </blockquote> <p>Yes. Other methods can access non-synchronized methods.</p> <blockquote> <p>Basically what I'm asking is everything in the synchronized method have a lock on it (including calls to other synchronized methods)? </p> </blockquote> <p>Uh, yes. Other calls to <em>synchronized</em> methods are locked. But non-synchronized methods are not locked.</p> <p>Also, remember that if the method is <code>static</code> then the lock is on the <code>Class</code> object in the <code>ClassLoader</code>.</p> <pre><code>// this locks on the Class object in the ClassLoader public static synchronized void someStaticMethod() { </code></pre> <p>If the method is an instance method then the lock is on the instance of the class.</p> <pre><code>// this locks on the instance object that contains the method public synchronized void someInstanceMethod() { </code></pre> <p>There are 2 different locks in those 2 cases.</p> <p>Lastly, when you are dealing with <code>synchronized</code> instance methods, each <em>instance</em> of the class is what is locked. This means that two threads could be in the same <code>synchronized</code> method at the same time with different <em>instances</em>. But if 2 threads try to operate on <code>synchronized</code> methods on the same instance, one will block until the other one exits the method.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload