AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Threads question

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Zoo 9000
Guest





PostPosted: Fri Jul 25, 2003 5:21 am    Post subject: Threads question Reply with quote



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);
}
});
}
}
Back to top
Roedy Green
Guest





PostPosted: Fri Jul 25, 2003 7:56 am    Post subject: Re: Threads question Reply with quote



On 24 Jul 2003 22:21:36 -0700, [email]zoo9000 (AT) hotmail (DOT) com[/email] (Zoo 9000) wrote or
quoted :

Quote:
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.


I make a stab at explaining this at
http://mindprod.com/jgloss/thread.html
and http://mindprod.com/jgloss/threadsafe.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.