user3105533
user3105533

Reputation: 331

wait() and notify() and an ActionListener

I have a graphic interface that works with two threads, each thread has to print a counter in their respective text area. The second thread(named threadEvent) works with an ActionListener and a button that blocks and unblocks the thread. When the user pushes the button it blocks the threadEvent(it stops printing thecounter), and when the button is pushed again it unblocks and continues printing in the respective textArea. For this, I have to block the thread with wait() and unblocked it with notify(), I have read some information about this but I don´t know how to use them with the button

 class T implements Runnable{
    private boolean print = true;
    private int i = 0;
    private long pause;
    private JTextArea textArea;

    T(long miliseconds,JTextArea textAreax){
        pause = miliseconds;
        textArea = textAreax;
    }

    public void pressedButton()
    {
        if(print)
           print = false;
        else
          print = true;
    }

    public void run()
    {
        while(print)
        {
            try
            {
                this.printCounter();
                Thread.sleep(pause);
                this.i++;
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void printCounter(){
        String time;
        time = Integer.toString(i);
        textArea.setText(time);  
    }
}

class Interface extends JFrame implements ActionListener{
    private JTextArea textArea,textArea2;
    private JButton button;
    private T thread,threadEvent;
    private Thread t,tE;

    Interface()
    {
        textArea = new JTextArea(10,7);
        textArea2 = new JTextArea(10,7);
        thread = new T(2000,textArea);
        threadEvent = new T(1000,textArea2);
        button = new JButton("Block/Unblock");
        this.getContentPane().add(button,BorderLayout.SOUTH);
        this.getContentPane().add(textArea,BorderLayout.WEST);
        this.getContentPane().add(textArea2,BorderLayout.EAST);

        t = new Thread(thread);
        tE = new Thread(threadEvent);

        t.start();
        tE.start();

        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event)
    {
        threadEvent.pressedButton();
    }       
}

public class MessageThreads{

    public static void main(String[] args) {
        Interface i = new Interface();
        i.setBounds(200, 200, 300, 240);
        i.setVisible(true);
    }

}

Upvotes: 0

Views: 922

Answers (1)

dsh
dsh

Reputation: 12213

A note regarding Swing and threads: Swing is not thread-safe. You can update Swing components (eg textArea.setText(time);) only from the event dispatch thread.

You do this using either SwingUtilities.invokeLater() or SwingUtilities.invokeAndWait(). For example:

SwingUtilities.invokeLater( new Runnable() {
    @Override
    public void run()
        { `textArea.setText(time); }
    });

PS. I know this is more of a comment than an answer, but it is too much to post as a comment

Upvotes: 3

Related Questions