1

I have build a GUI for the popurse that when I click the checkbox button,the corresponding thread will suspend or wake up.But it doesn't come out with the expected result.

class itemStateChangedHandler implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
        JCheckBox jcheckbox = (JCheckBox) e.getItem();
        String s = jcheckbox.getText();
        if (jcheckbox.isSelected() == true) {
            try {
                switch (s) {
                    case "Thread 1 suspend":
                        synchronized(t1) {
                            t1.wait();
                        }
                        break;
                    case "Thread 2 suspend":
                        synchronized(t2) {
                            t2.wait();
                        }
                        break;
                    case "Thread 3 suspend":
                        synchronized(t3) {
                            t3.wait();
                        }
                        break;
                }
            } catch (InterruptedException ex) {}
        } else {
            switch (s) {
                case "Thread 1 wake up":
                    synchronized(t1) {
                        t1.notify();
                    }
                    break;
                case "Thread 2 wake up":
                    synchronized(t2) {
                        t1.notify();
                    }
                    break;
                case "Thread 3 wake up":
                    synchronized(t3) {
                        t1.notify();
                    }
                    break;
            }
        }
    }
}`
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
J.Fong
  • 11
  • 2
  • Please forgive my worst formatting...It's my first time to use it.. – J.Fong Dec 06 '15 at 15:50
  • no problem but is it possible to explain why you're stuck? What is the problem exactly and what did you try to solve it? – Yassin Hajaj Dec 06 '15 at 15:51
  • It's when you come accross wait method, your listener would hang up. What are you trying to achieve with this code? – SMA Dec 06 '15 at 15:52
  • I'm guessing that `t1.wait()` and `t1.notify()` do not do what you think they do. For starters, `t1.wait()` literally does nothing at all to thread `t1`. You might want to work through the Java concurrency tutorial https://docs.oracle.com/javase/tutorial/essential/concurrency/ and pay special attention to the part where it talks about _guarded blocks_. – Solomon Slow Dec 06 '15 at 19:51
  • I've created a class which is implements the runnable interface which is used to create random numbers constantly.Then I made three threads to test this class.What I want to do is that when I click a button the corresponding thread like t1 will be suspended and will be wake up when I click the button again.But the question is when I click the button,It will invoke the method like t1.wait() for example,the whole program seems like being suspended,what I want is the thread,t1 to be suspended and the other two thread is still running.It confuses me a lot. – J.Fong Dec 08 '15 at 16:44

1 Answers1

0

You should call “wait()” inside your run() method. Take a look at “myThread” class here:

How to handle termination and suspension of multiple Java Threads in JFrame?

A boolean variable “running” is used to control the suspension of a thread.

Jarvis
  • 117
  • 1
  • 1
  • 10