Results 1 to 2 of 2
- 04-28-2011, 05:47 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
All Threads in Blocked State ( Program Hangs )
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.
Java Code:import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.HashMap; import java.util.Vector; import java.util.Map.Entry; public class FileQueue { private LinkedList<Integer> nextFile; private HashMap<Integer,LinkedList<String>> terminalQueue; int fileCount; FileQueue(){ fileCount = 0; nextFile = new LinkedList<Integer>(); terminalQueue = new HashMap<Integer,LinkedList<String>>(); } FileQueue(Vector<String> files){ this(); Collections.sort(files); Iterator<String> it = files.iterator(); while(it.hasNext()) this.pushFile(it.next()); } public boolean hasFiles(){ return (fileCount>0)?true:false; } public void pushFile(String filePath){ int lastSlashIndex = filePath.lastIndexOf("/"); String fileName = filePath.substring(lastSlashIndex+1); int terminalId = Integer.parseInt(fileName.substring(19,fileName.lastIndexOf("_")).trim()); LinkedList file = terminalQueue.get(terminalId); if(file==null) { synchronized(this){ nextFile.addLast(terminalId); LinkedList<String> newList = new LinkedList<String>(); newList.addLast(filePath); terminalQueue.put(terminalId,newList ); fileCount++; } } else { synchronized(this){ terminalQueue.get(terminalId).addLast(filePath); fileCount++; } } } public synchronized void processed(int terminalId)throws InterruptedException{ if(terminalQueue.containsKey(terminalId)) nextFile.addLast(terminalId); notify(); } public synchronized String popFile() throws InterruptedException{ if(nextFile.size()==0) wait(); int terminalId = nextFile.poll().intValue(); LinkedList<String> queueFileNames = terminalQueue.get(terminalId); fileCount--; String retValue = queueFileNames.poll(); if(queueFileNames.size()==0) terminalQueue.remove(terminalId); return retValue; }
In the main program the code is being called this way
Java Code:public void run(){ while(fileQueue.hasFiles()){ String retname = fileQueue.popFile(); // Process file with name stored in retname } }
Thanks in advance
- 08-09-2011, 12:08 AM #2
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
Similar Threads
-
How to release multiple threads from waiting state to runnable state
By Dayanand in forum New To JavaReplies: 2Last Post: 02-14-2011, 02:27 PM -
Using threads in a graphical program
By jameskelly in forum New To JavaReplies: 6Last Post: 11-28-2010, 03:52 PM -
Threads BLOCKED by sun.security.pkcs11.wrapper.PKCS11.C_CloseSession
By abru in forum NetworkingReplies: 0Last Post: 10-04-2010, 12:57 PM -
How to interrupt threads blocked in a RMI operation
By sky in forum Advanced JavaReplies: 2Last Post: 03-19-2010, 08:24 AM -
Program hangs
By yuliada in forum Advanced JavaReplies: 2Last Post: 11-11-2009, 03:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks