Threads question

Z

Zoo 9000

I've been trying to understand how wait() and notify() work
but even Sun's own explanation (here,
http://java.sun.com/products/jdk/1.2/docs/guide/misc/threadPrimitiveDeprecation.html
) isn't clear and doesn't give a working example.

So I've tried to build a simple one myself. The program (below)
compiles and runs under J2SE 1.4.1 but throws an
'IllegalMonitorStateException' (whatever that is..). If anyone can
show me where I'm going wrong I'd be grateful.

- Ken


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class VisualWaitNotify
extends JPanel
implements Runnable {

private static final String[] symbolList =
{ "|", "/", "-", "\\", "|", "/", "-", "\\" };

private Thread runThread;
private JTextField symbolTF;
private boolean threadSuspended = false;

public VisualWaitNotify() {
symbolTF = new JTextField();
symbolTF.setEditable(false);
symbolTF.setFont(new Font("Monospaced",
Font.BOLD, 26));
symbolTF.setHorizontalAlignment(JTextField.CENTER);
final JButton waitB = new JButton("Wait");
final JButton notifyB = new JButton("Notify");

waitB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
waitNow();
}
});

notifyB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
notifyNow();
}
});

JPanel innerStackP = new JPanel();
innerStackP.setLayout(new GridLayout(0, 1, 3, 3));
innerStackP.add(symbolTF);
innerStackP.add(waitB);
innerStackP.add(notifyB);

this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(innerStackP);
}
private void waitNow() {

threadSuspended = true;

try {
synchronized (this) {
while (threadSuspended)
runThread.wait();
}
} catch (InterruptedException e){
}
}

private synchronized void notifyNow() {

threadSuspended = false;

if ( runThread != null ) {
runThread.notify();
}
}

public void run() {
try {
runThread = Thread.currentThread();
int count = 0;

while ( true ) {
// each time through, show the next symbol
symbolTF.setText(
symbolList[ count % symbolList.length ]);
Thread.sleep(200);
count++;
}
} catch ( InterruptedException x ) {
// ignore
} finally {
// The thread is about to die, make sure
// that the reference to it is also lost.
runThread = null;
}
}

public static void main(String[] args) {
VisualWaitNotify vwn = new VisualWaitNotify();
Thread t = new Thread(vwn);
t.start();

JFrame f = new JFrame("Visual Wait and Notify");
f.setContentPane(vwn);
f.setSize(320, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
 
R

Roedy Green

In simple terms, wait() causes the running thread to pause until it is
notified by some other thread via notify() or notify() all that it should
wake up.

Think in terms of what happens in a barbershop with multiple customers
and multiple barbers. Customers call wait() when all barbers are
busy. And a barber who is free calls notify() to wake up one of the
waiting customers.
 
R

Roedy Green

N

Nigel Wade

Roedy said:
what were you recommending there? The only thing I could see was
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

That's the one.

Sorry, didn't see that link right down at the bottom...
This is a complicated topic and all the material we can gather the
better.

It certainly is. The brain doesn't seem to be designed to think in parallel,
at last not in logical terms and with propper synchronization mechanisms.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top