开发者

Problem while creating Singleton

I am trying to create a Singleton class, which will be accessed from two other classes.Can anyone please tell me whats wrong with the following code? I am just not able to figure out!

import java.util.LinkedList;

public class MessageQueue {

    private static final LinkedList<ServerDataEvent> queue = new LinkedList<ServerDataEvent>();;

    private static MessageQueue messageQueue = null;

    /** A private Constructor prevents any other class from instantiating. */
    private MessageQueue() {
    }

    /** Static 'instance' method */
    public static MessageQueue getInstance() {
        if (MessageQueue.messageQueue == null) {
            System.out.println("Creating MessageQueue instance.");
            MessageQueue.messageQueue = new MessageQueue();
        }
        return MessageQueue.messageQueue;
    }

    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}

I am accessing the singleton object from other classes like this:

MessageQueue messageQueue = MessageQueue.getInstance();

There are no errors, but

System.out.println("Creating MessageQueue instance.");

is getting executed whenever I do

MessageQueue messageQueue = MessageQueue.getInstance();

EDIT 1

import java.util.LinkedList;

public class MessageQueue {

    private static final LinkedList<ServerDataEvent> queue = new LinkedList<ServerDataEvent>();;

    private static final MessageQueue messageQueue = new MessageQueue();

    /** A private Constructor prevents any other class from instantiating. */
   开发者_开发技巧 private MessageQueue() {
        System.out.println("problem...");
    }

    /** Static 'instance' method */
    public static MessageQueue getInstance() {
        return MessageQueue.messageQueue;
    }

    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}


First of all, you did not specify any errors you get. If you want to get help, you should give us as much information as you can.

Secondly, the best fool-proof way to create a singleton in Java is this:

public enum MySingleton {
    INSTANCE;

    //whatever methods you want to implement
}

and you access it like so: MySingleton.INSTANCE.whatever().


It is much better to define and instantiate your singleton object like this:

private static final MessageQueue messageQueue = new MessageQueue();

And then getInstance will be just:

public static MessageQueue getInstance() {
   return MessageQueue.messageQueue;
}

This way your singleton object is instantiated and will be thread safe because it is created by the class loader.


A shorter version which is thread safe.

public enum MessageQueue {
    INSTANCE;

    private final Queue<ServerDataEvent> queue = 
        new ConcurrentLinkedQueue<ServerDataEvent>();    

    public void addEvent(ServerDataEvent event) { queue.add(event); }
}

or

public enum MessageQueue {
    ;

    private static final Queue<ServerDataEvent> queue = 
        new ConcurrentLinkedQueue<ServerDataEvent>();    

    public static void addEvent(ServerDataEvent event) { queue.add(event); }
}


would be easier if you did it this way...

public class MessageQueue {

    private static final MessageQueue INSTANCE= new MessageQueue();

    public static MessageQueue getINSTANCE() {
        return INSTANCE;
    }

    private MessageQueue() {
    }
}


What's the error that occurs? All I can see from this is you have two semicolons here:

private static final LinkedList<ServerDataEvent> queue = new LinkedList<ServerDataEvent>();;


Simplest method:

private static final MessageQueue messageQueue = new MessageQueue();

public static MessageQueue getInstance() {
   return MessageQueue.messageQueue;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜