• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread won't resume

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic