• Post Reply Bookmark Topic Watch Topic
  • New Topic

thread won't resume  RSS feed

 
Michael Redding
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i've been having the problem for

when array list is less than the size of 3 (it counts the threads so when 3rd thread runs) I want the first 2 threads to wait until the 3rd one is running, the arraylists size increases when each thread is run so that is why I have if (somearraylist.size() < 3), so on the 3rd one it should notify all the other threads to stop waiting and continue, but it isn't working since the 1st 2 threads don't continue. I've even tried synchronized(somearraylist) { } with everything inside it inside the run and when i do that i get a monitorstateexception instead. Can someone tell me how to fix this or to have any other way for when the 3rd thread is run the other 2 continue ?

Here is the code:



Thanks
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I hope this doesn't double post. I had a connection problem...

When you use wait(); then you are calling the wait(); on the current Object. Are all your Runnables exactly the same instance of the Runnable? If not, then you can not notify them. Also are you sure the ArrayList is growing the way you expect it to? Maybe you are working with the wrong list?

Assuming you indeed have the proper ArrayList, but are using different instances of the Runnable, you should synchronize, wait(), and notify() on the ArrayList itself, since that would be shared between all threads.


However, I suggest you use the java.util.concurrent.CountDownLatch instead. The CountDownLatch will handle the synching and notifying for you, and is easier to read/maintain:



Note that I did not add any error handling, so make sure you add all the necessary try/catch statements.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!