So far, there's no garranty that a blocked Thread (such in Thread.State BLOCKED) would come to resume its state to RUNNING, but the waiting Thread (in Thread.WAITING) will surely resume on notify(). In fact if notify() and wait() are used with care, you get the correct Thread wait and continue behaviour. Without it, that would make no sense to synchronize, as by the definition it can be incomplete if left unmanaged...

So to "take a tour" in this complex stuff for beginners, I've made a simple application by myself to illustrate better what Synchronization IS and ISN'T. compile with :
shell-cmd$ javac MainLockT.java and run :
shell-cmd$ java MainLockT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class MainLockT { static String statusThreadLock(Object lock, Thread t) { String s = (t.holdsLock(lock))?t.getName() + " holds lock on lock " + lock.toString():t.getName() + " doesn't hold lock on lock " + lock.toString(); System.out.println(s); text.append(s+ "\r\n"); sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getMaximum()); text.repaint(); return s; }
static Thread getNotifier() { Thread t2 = new Thread(new Runnable() { public void run() { statusThreadLock(lock, Thread.currentThread()); try { synchronized(lock) { JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread())); final long timer = System.currentTimeMillis(); JFrame f = new JFrame(); JComponent c; f.getContentPane().add(c = new JComponent() { public void paint(Graphics g) { super.paint(g); double rest = 5.0 - (double)(System.currentTimeMillis() - timer) / 1000.0; g.drawString((int)rest + " sec", 10, (int)((double)(this.getHeight() - g.getFontMetrics().getHeight()) / 2.0)); }}); c.setPreferredSize(new Dimension(100, 50)); f.pack(); f.setLocationRelativeTo(text); long wait = 0; long i = (long)(1000.0 / 24.0); while(5000 > (wait += i)) { f.setVisible(true); Thread.sleep(i); f.repaint(); } lock.notify(); f.setVisible(false); f.dispose(); } } catch(InterruptedException e) { e.printStackTrace(); }
}}, "5sec-NOTIFIER THREAD"); return t2; } static Thread getWaiter() { Thread t0 = new Thread(new Runnable () { public void run() { statusThreadLock(lock, Thread.currentThread()); try { synchronized(lock) { JFrame f = new JFrame("wait"); f.pack(); f.setLocationRelativeTo(text); JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread())); f.setVisible(true); lock.wait(); f.setVisible(false); f.dispose(); } JOptionPane.showMessageDialog(text, statusThreadLock(lock, Thread.currentThread())); } catch(InterruptedException e) { e.printStackTrace(); } }}, "NOTIFY WAITER THREAD"); return t0; }
static boolean esc = false; static JTextArea text; static JScrollPane sp; final static Object lock = new Long(System.currentTimeMillis());
public static void main(String[] args) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() { public boolean postProcessKeyEvent(KeyEvent e) { if(e.getID() == KeyEvent.KEY_PRESSED) if(e.getKeyCode() == KeyEvent.VK_ESCAPE) return esc = true; return false; }});
JFrame f = new JFrame(); f.getContentPane().setLayout(new FlowLayout()); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); Action action_notify = new AbstractAction("<NOTIFY>") { public void actionPerformed(ActionEvent e) { getNotifier().start(); }}; Action action_notifyAll = new AbstractAction("<NOTIFY ALL>") { public void actionPerformed(ActionEvent e) { new Thread(new Runnable() {public void run() { synchronized(lock) { lock.notifyAll(); } }}).start(); }}; Action action_wait = new AbstractAction("<WAIT>") { public void actionPerformed(ActionEvent e) { getWaiter().start(); }}; sp = new JScrollPane(text = new JTextArea()); f.getContentPane().add(sp); sp.setPreferredSize(new Dimension(300, 150)); text.setLineWrap(true);
f.getContentPane().add(new JButton(action_notify)); f.getContentPane().add(new JButton(action_wait)); f.getContentPane().add(new JButton(action_notifyAll)); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
Thread t1 = new Thread(new Runnable() { public void run() { statusThreadLock(lock, Thread.currentThread()); synchronized(lock) { try { while(!esc) { System.out.println("press Escape to exit !"); lock.wait(500); } statusThreadLock(lock, Thread.currentThread()); System.exit(0); } catch(InterruptedException e) { e.printStackTrace(); } } }}, "EVENT WAITER THREAD"); t1.start(); } } |
