One Test Application for synchronized() wait()-notify() blocks

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… ::slight_smile: 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

http://membres.lycos.fr/broumbroum23/image_phps/ThreadLocks.jpg

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);
/** run 2 threads in the correct order */
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());
		//lock.notify();
		System.exit(0);
		} catch(InterruptedException e) { e.printStackTrace(); }    
             }
}}, "EVENT WAITER THREAD");
t1.start();
}
}