Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

All Threads in Blocked State ( Program Hangs )  RSS feed

 
Akash Ashok
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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


 
Stephan van Hulst
Saloon Keeper
Posts: 6978
110
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!