Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

All Threads in Blocked State ( Program Hangs )

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a basic data structure FileQueue which contains a linked list and a hash map. I am inserting data into to it as a single thread .. but multiple threads access popFile and processed methods which are synchronized. I have an issue with this .. and at sometime all threads go into blocked state and the program hangs . I am not able to figure out why could some1 help me out.



In the main program the code is being called this way


 
Saloon Keeper
Posts: 13860
313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Akash, welcome to CodeRanch.

It looks like all the threads are waiting at popFile(). Can you guarantee that there are other threads calling processed() to wake them up? If not, you have guaranteed deadlock.

Also, you are calling notify(). Don't call notify(). Use notifyAll() instead.

[edit]

You may also want to use a while statement to check whether wait() should be called. Never call wait() from an if statement, it's not reliable.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Never call wait outside a loop, a good bug finder program will flag that one ..

wait description

- Reads of fileCount should be synchronized or you may not see the current value (ever) i.e. synchronize hasFiles ()

- pushFile calls get on terminalQueue with no memory barrier, HashMap isn't thread safe, reads AND writes should be from sync blocks if you want to use multiple threads !


Can I suggest you simply the whole thing down to synchronizing all the methods maybe (I'd avoid small sync blocks of code as invariably the author gets it wrong, yes I know performance but thats only useful if your code is correct), could you not just synchronize pushFile if you get dead lock you can get a stack trace from the OS that will list all the threads and why they're blocked <ctrl><break> Windows, post that and it should be a lot more straight forward to see why your deadlocked.
 
Akash Ashok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks a lot !!!
I put the wait() inside while loop and notifiedAll().
The program pretty much started workin fine except for a deadlock on a DB connection which is unrelated to this. Thanks a lot of the help
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic