Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other questions have focused on what the language says that <code>wait</code> and <code>notify</code> are - but that doesn't seem to be what your question is about... you talk about mutexes, which is an implementation detail and therefore JVM specific.</p> <p>So we need to pick a JVM - let's pick openjdk (source available <a href="http://openjdk.java.net/projects/jdk6/" rel="noreferrer">here</a>). The bit of code that (ultimately) handles all this stuff is found at <code>hotspot/src/share/vm/runtime/objectMonitor.cpp</code>.</p> <p>This maintains two datastructures - a wait set and an entry set. Waiting threads are added to the wait set and parked whereas threads attempting to take the monitor are added to the entry set and then parked. On <code>notify</code> a thread is taken from the wait set and added to the entry set. When a thread releases the lock it unparks a thread from the entry set if there is one. Note that these sets are actually implemented as queues (linked lists) so are treated on a FIFO basis.</p> <p>Therefore, in this particular case the implementation treats waiting on an object's monitor and attempting to take an object's monitor in a similar way.</p> <p>But this is just one implementation of one JVM (although it's likely that others do something similar) - so we cannot rely on it. So I suppose the question is why do you want to know? If it's just curiosity then look through the openjdk code, it's fascinating. If you plan on using this information in your code... don't.</p> <p><strong>UPDATE</strong></p> <p>I realise that saying "park" doesn't tell us much. The code that parks a thread is platform specific (and is implemented in an object called <code>PlatformEvent</code>, which <code>ParkEvent</code> extends). In the version of openjdk that I'm looking at the park code for linux can be found at <code>hotspot/src/os/linux/vm/os_linux.cpp</code> and this calls <code>pthread_mutex_lock(_mutex)</code>... so in answer to your question <strong>yes</strong> calling wait <em>may</em> take a mutex <em>on my machine</em>. Note that there's lots of stuff that happens above this which might prevent us getting to this point.</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.
    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