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
- 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.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
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