Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads question

Reply
Thread Tools

Threads question

 
 
Zoo 9000
Guest
Posts: n/a
 
      07-25-2003
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...precation.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);
}
});
}
}
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-25-2003
On 24 Jul 2003 22:21:36 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Zoo 9000) wrote or
quoted :

>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...precation.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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-26-2003
On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <(E-Mail Removed)>
wrote or quoted :

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


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-26-2003
On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <(E-Mail Removed)>
wrote or quoted :

>That's not the best reference. Read Roedy's references mentioned in his post
>and also look at the Java tutorial "Essential Java Classes"
>http://java.sun.com/docs/books/tutor...ial/index.html


what were you recommending there? The only thing I could see was
http://java.sun.com/docs/books/tutor...ads/index.html
which is listed already in http://mindprod.com/jgloss/threads.html

This is a complicated topic and all the material we can gather the
better.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      07-28-2003
Roedy Green wrote:

> On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <(E-Mail Removed)>
> wrote or quoted :
>
>>That's not the best reference. Read Roedy's references mentioned in his
>>post and also look at the Java tutorial "Essential Java Classes"
>>http://java.sun.com/docs/books/tutor...ial/index.html

>
> what were you recommending there? The only thing I could see was
> http://java.sun.com/docs/books/tutor...ads/index.html


That's the one.

> which is listed already in http://mindprod.com/jgloss/threads.html
>


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.

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : (E-Mail Removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments