Java threading problem

Posted by Krt_Malta on Stack Overflow See other posts from Stack Overflow or by Krt_Malta
Published on 2010-03-16T17:58:31Z Indexed on 2010/03/16 18:01 UTC
Read the original article Hit count: 125

Filed under:
|
|
|

Hi!

I'm using multiple threads in my application. Basically I have a combo box and upon selecting Inbox, p1 resumes and p2 is suspended and upon selecting Send, p2 starts and p1 stops. Below is the code (I'm sure it's not perfect)

public void modifyText(ModifyEvent e) {
                if (combo.getText().equals("Inbox"))
                {
                    synchronized(p2) 
                    {
                        p2.cont = false;
                    }
                    table.removeAll();
                    synchronized(p1)
                    {
                        p1.cont = true;
                        p1.notify();
                    }
                }


                else if (combo.getText().equals("Sent"))
                {
                    synchronized(p2) 
                    {
                        p1.cont = false;
                    }
                    table.removeAll();
                    synchronized(p1)
                    {
                        p2.cont = true;
                        p2.notify();
                    }
                }
            }
        });

and for P1 and P2 I have this inside their while loops:

synchronized (this) {
            while (cont == false)
                try {
                    wait();
                } catch (Exception e) {
                }
        } 

... As it is it's now working (I'm a beginner to threads). On pressing Sent in the combo box, I get an IllegalStateMonitorException. Could anyone help me solve the problem plz?

Thanks and regards, Krt_Malta

© Stack Overflow or respective owner

Related posts about java

Related posts about threads