Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>The object that you are synchronizing on in this example is not the class, but the instance, so each new <code>Test2</code> object would be synchronizing on a different monitor.</li> <li>The method you might be looking for here is <code>sleep</code>, not <code>wait</code>. <code>wait</code> will release the lock that <code>synchronized</code> has grabbed, and will be re-gotten once the thread is notified.</li> </ol> <p>Note that for your test to work correctly, you will need to lock on a common object. If you want to see <code>wait</code> in action, I have thrown together a simple app that will pop up a frame with a "Notify" button. Two threads will be started that wait on a common object and are in turn notified when the button is pressed.</p> <pre><code>public static void main(String[] args) { final Object lock = new Object(); final JFrame frame = new JFrame("Notify Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Notify"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { synchronized(lock) { lock.notify(); } } }); frame.add(button); SwingUtilities.invokeLater(new Runnable() { public void run() { frame.setVisible( true ); } }); new Thread(new Runnable() { public void run() { synchronized(lock) { try { System.out.println("1. starting"); lock.wait(); System.out.println("1. step 1"); lock.wait(); System.out.println("1. step 2"); } catch (InterruptedException ie) { ie.printStackTrace(); } } } }).start(); new Thread(new Runnable() { public void run() { synchronized(lock) { try { System.out.println("2. starting"); lock.wait(); System.out.println("2. step 1"); lock.wait(); System.out.println("2. step 2"); } catch (InterruptedException ie) { ie.printStackTrace(); } } } }).start(); } </code></pre> <p>For a simple explanation of <code>wait</code>, the JavaDoc is always a good place to start:</p> <blockquote> <p>Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).</p> <p><strong>The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.</strong> </p> </blockquote>
    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