Threads question

Discussion in 'Java' started by Zoo 9000, Jul 25, 2003.

  1. Zoo 9000

    Zoo 9000 Guest

    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);
    }
    });
    }
    }
     
    Zoo 9000, Jul 25, 2003
    #1
    1. Advertising

  2. Zoo 9000

    Roedy Green Guest

    On 24 Jul 2003 22:21:36 -0700, (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/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.
     
    Roedy Green, Jul 25, 2003
    #2
    1. Advertising

  3. Zoo 9000

    Roedy Green Guest

    On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <>
    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.
     
    Roedy Green, Jul 26, 2003
    #3
  4. Zoo 9000

    Roedy Green Guest

    On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <>
    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/tutorial/essential/index.html


    what were you recommending there? The only thing I could see was
    http://java.sun.com/docs/books/tutorial/essential/threads/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.
     
    Roedy Green, Jul 26, 2003
    #4
  5. Zoo 9000

    Nigel Wade Guest

    Roedy Green wrote:

    > On Fri, 25 Jul 2003 14:52:42 +0100, Nigel Wade <>
    > 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/tutorial/essential/index.html

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

    > 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 :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Jul 28, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Marden
    Replies:
    0
    Views:
    1,074
    Nick Marden
    Jan 17, 2004
  2. yoda
    Replies:
    2
    Views:
    464
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    433
    William Ahern
    Aug 27, 2004
  4. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,485
    Arne Vajhøj
    Apr 9, 2008
  5. Une bévue
    Replies:
    0
    Views:
    176
    Une bévue
    Jun 14, 2006
Loading...

Share This Page