Problem with Java threads

I use multiple threads in my application. Basically, I have a combo box and when I select the Inbox p1 resumes and p2 pauses, and after I select Send p2 starts and p1 stops. Below is the code (I'm sure it is 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) { } } 

... How it works now (I'm new to threads). When I click Submitted in the combo box, I get an IllegalStateMonitorException. Can someone help me solve plz problem?

Thank you and welcome Krt_Malta p>

+4
source share
3 answers

the problem is here:

 synchronized(p1) { p2.cont = true; p2.notify(); } 

You execute p2.notify() when you do not have a lock on p2 (you must hold the monitor to trigger a notification about this). Change synchronized(p1) to synchronized(p2) . In addition, you also need to cancel another synchronized offer, which is also defective. So, as an example:

 synchronized(p1) { p1.cont = false; // p1.notify(); <- do you need this here? } table.removeAll(); synchronized(p2) { p2.cont = true; p2.notify(); } 

Also, your other code is also a bit wrong, a very bad practice of locking inside an entire loop, make it a little more atomic.

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

Additional optimization, avoid synchronised if possible:

 if (p1.cont) { synchronized(p1) { p1.cont = false; // p1.notify(); <- do you need this here? } } table.removeAll(); if (!p2.cont) { synchronized(p2) { p2.cont = true; p2.notify(); } } 

Make a cont volatile field here and a mirror for the other part of the if statement, if necessary.

Edit: Looking back and battling with the concurrency error that I recently encountered, anyone who implements this template may encounter an endless wait problem if an object locked and half-locked is being scanned for the while loop condition (this is because there is a space, in which the state can change between the evaluation of the conditional and the imposition of the wait operator) In this case, place the synchronized block outside the loop.

+6
source

In this code

  synchronized(p1) { p2.cont = true; p2.notify(); } 

You synchronize on p1 , but call notify() on p2 , which leads to an exception.

0
source

You cannot wait in the thread for sending awt events or otherwise, which will delay your entire application. Read about http://en.wikipedia.org/wiki/Event_dispatching_thread

Also, you should not use raw streams unless you know what you are doing. Check out http://java.sun.com/javase/6/docs/api/java/util/concurrent/package-summary.html and read the Executors

-1
source

Source: https://habr.com/ru/post/1304294/


All Articles