Results 1 to 4 of 4
- 05-10-2012, 11:47 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 50
- Rep Power
- 0
How to prevent infinite loop when using Singleton pattern?
Guys how to prevent infinite loop when using singleton pattern?
here is my problems:(
this the another class and it goes infinite loopJava 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; } }
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(); }*/ }
-
Re: How to prevent infinite loop when using Singleton pattern?
What line(s) are involved in your problem?
- 05-11-2012, 09:58 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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:
and then change the getInstance to:Java Code:private static Library instance = new Library();
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.Java Code:public static Library getInstance() { return instance; }Please do not ask for code as refusal often offends.
- 05-11-2012, 04:22 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 50
- Rep Power
- 0
Re: How to prevent infinite loop when using Singleton pattern?
Similar Threads
-
Please help with Singleton pattern.
By fatabass in forum New To JavaReplies: 19Last Post: 03-10-2012, 07:00 PM -
Singleton Pattern
By ShaileshRaj in forum Advanced JavaReplies: 5Last Post: 02-29-2012, 04:52 PM -
singleton design pattern
By ziaur25@gmail.com in forum Advanced JavaReplies: 2Last Post: 02-25-2011, 08:28 PM -
Singleton Pattern
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:21 PM -
singleton pattern
By Peter in forum Advanced JavaReplies: 1Last Post: 07-09-2007, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks