• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Thread causing Hang ??

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code, I am creating 5 thread here..all are executing till extinct..but one(sometimes 2 etc) thread is going for permanent wait..causing My system to hang..!! Can some one please help me to find out ..why this is happening and what is the solution to avoid this problem..!!

 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sunny,
The problem is that you are incrementing the counter before getting into wait() (compounding the problem you do it before getting into synchronized block.) So, the counter does not really tell the number of threads waiting on the mutex. Thus, the notifyAll() in many cases will be called even before all the threads have gone into the waiting state and thus, the threads getting into wait() after notifyAll() will be stuck forever and never get a notify call.
A simple soultion can be to keep on sending notification till all the threads have received it. This would mean that the counter will be incremented after return from wait and the notification thread keeps on notifying till the counter is 5 !!!
(No points in telling that this is the most stupid way of handling wait-notify! But the logic you have is not something that a real system will have.)
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot..!! I understand what you are trying to say..Actually I am practicing Threading so that I can apply them to real system..!! I do not feel very comfortable in Threading..!!
I was even not aware of Atomization in Java..!! Well now i have gone through all..and feeling very comfortable now..!!
 
We noticed he had no friends. So we gave him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic