Results 1 to 4 of 4
  1. #1
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default How to prevent infinite loop when using Singleton pattern?

    Guys how to prevent infinite loop when using singleton pattern?

    here is my problems:(
    Java Code:
    import java.io.IOException;
    import java.util.ArrayList;
    
    
    public class Library implements CollectionAggregate {
    	
    	public ArrayList <Collection> aggregate =new ArrayList<Collection>() ;
    	private FileReaderWriter file ;
    	private static  Library instance;
    	
    	public LibraryIterator iterator=null;
    	
    	public Library(String preventLoop){
    		
    	}
    	private Library() {
    		System.out.println(k);
    		
    		try {
    			
    			file=new FileReaderWriter();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		
    	}
    	
    	
    	public LibraryIterator iterator() {
    		iterator=new LibraryCollectionIterator();
    		return  iterator;
    	}
    
    	//degiscek....
    	public ArrayList<Collection> getAggregate() {
    		return aggregate;
    	}
    
    
    	public void addAggregate(Collection collection) {
    		aggregate.add(collection);
    		
    	}
    	public static Library getInstance() {
    		if (instance==null)
    			return instance=new Library();
    		else	return instance;
    	}
    	
    }
    this the another class and it goes infinite loop
    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    
    
    public class FileReaderWriter {
    	private PrintWriter writer;
    	private BufferedReader reader;
    	private String log;
    	private Library library;
    	
    	
    	
    	FileReaderWriter()throws IOException {
    		reader = new BufferedReader(new FileReader("c:\\LibraryData.txt"));
    		readContent();
    		//writer = new PrintWriter(new FileWriter("c:\\LibraryData.txt"),true);
    		
    		
    	}
    	public void readContent() throws IOException {
    		
    		String line="";
    		while((line=reader.readLine())!=null){  
    			String kayit=line;
    			StringTokenizer token = new StringTokenizer(kayit);
    			while(token.hasMoreTokens()){
    				String type =token.nextToken();
    				String name =token.nextToken();
    				String auther =token.nextToken();
    				String date =token.nextToken();
    				System.out.println(date);
    				int code =Integer.parseInt(token.nextToken());
    				Collection collection = new Collection(type,name,auther,date,code);
    				library=Library.getInstance();
    				library.addAggregate(collection);
    				
    			}
    			
    		}
    		
    
    }
    
    	
    	/*public void printReport(String report) {
    		
    		 writer.println(report);
    		 writer.flush();
    	
    				
    			
    	}*/
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: How to prevent infinite loop when using Singleton pattern?

    If you show us the stack trace for the overflow exception we should be able to show you exactly where the problem lies.
    As a start point, though, can you remove the public constructor for Library (Library(String)) and, for the 'instance' declaration do:
    Java Code:
    private static Library instance = new Library();
    and then change the getInstance to:
    Java Code:
    public static Library getInstance() {
        return instance;
    }
    I can't guarantee this will cure the loop, since you haven't posted the stack trace, but it will make your singleton neater and less prone to having the constructor called multiple times, which would cause a problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: How to prevent infinite loop when using Singleton pattern?

    Quote Originally Posted by Fubarable View Post
    What line(s) are involved in your problem?
    the problem is that it goes infinite loop that i have checked with system.out.println(" b" ); it did write b infitly and program had not given a error msg.

    so that i cant figure out where the problem occur :(

Similar Threads

  1. Please help with Singleton pattern.
    By fatabass in forum New To Java
    Replies: 19
    Last Post: 03-10-2012, 08:00 PM
  2. Singleton Pattern
    By ShaileshRaj in forum Advanced Java
    Replies: 5
    Last Post: 02-29-2012, 05:52 PM
  3. singleton design pattern
    By ziaur25@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2011, 09:28 PM
  4. Singleton Pattern
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 04:21 PM
  5. singleton pattern
    By Peter in forum Advanced Java
    Replies: 1
    Last Post: 07-09-2007, 05:45 AM

Posting Permissions

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