Results 1 to 2 of 2
  1. #1
    thehellmaker is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default 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

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    More code?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2011, 03:27 PM
  2. Using threads in a graphical program
    By jameskelly in forum New To Java
    Replies: 6
    Last Post: 11-28-2010, 04:52 PM
  3. Replies: 0
    Last Post: 10-04-2010, 01:57 PM
  4. Replies: 2
    Last Post: 03-19-2010, 09:24 AM
  5. Program hangs
    By yuliada in forum Advanced Java
    Replies: 2
    Last Post: 11-11-2009, 04:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •