0

I think I understand the Singleton pattern. It seems to me that there are many cases where more than one user is accessing the single instance that the pattern enforces. But is there a refinement that prevents that instance from being used (even read-only) while another user is accessing it or is that still a Singleton?

EDIT: So if a Singleton does not prevent more than one thread from accessing it, is there a standard way to enforce this further functionality. To be clear, if a thread attempts to get the instance before the first thread that accessed the Singleton is done, an exception is thrown or the thread blocks?

Jeff
  • 1,513
  • 4
  • 18
  • 34
  • Are you calling a `Thread` a user? – dotvav Nov 09 '15 at 10:37
  • A thread-safe Singleton has one `getInstance()` method which returns the instance (optionally constructing it if it is not already constructed). All other threads will have to wait to get access to the reference - irrespective of what they want to do with the instance – TheLostMind Nov 09 '15 at 10:37

3 Answers3

0

A Singleton can only be instantiated once. This says nothing about the number of (simultaneous) users it has.

What you say: a class which instance can have only one user, this means: per user, there is a different instance, not that only one user can use it.

So: there's a big difference there.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

No. The singleton pattern makes sure that there's only one instance of a class. It does not do any kind of synchronization.

You are of corse free to implement this in your class by using synchronized methods.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
0

You can use this aproach of thread safe singleton:

https://stackoverflow.com/a/16106598/327786

public class CassandraAstyanaxConnection {
    private static class Holder {
        static final CassandraAstyanaxConnection INSTANCE = new CassandraAstyanaxConnection();
    }

    public static CassandraAstyanaxConnection getInstance() {
        return Holder.INSTANCE;
    }
    // rest of class omitted
}
Community
  • 1
  • 1
TlmaK0
  • 3,578
  • 2
  • 31
  • 51
  • TimaKO, what happens to the second thread that attempts to obtain the instance? – Jeff Nov 09 '15 at 11:07
  • The instance is already created when you call the first time. So every call will return the same instance. – TlmaK0 Nov 09 '15 at 11:13
  • But I think I want the second thread to block or get an exception if it tries to access: I want only one thread accessing the Singleton at a time. Something like an "In Use" flag that every thread looks at before attempting to get an instance. – Jeff Nov 09 '15 at 11:33
  • As says @Bohemian in the link, the class loader loads classes when they are first accessed. When a class is loaded, and before anyone can use it, all static initializers are guaranteed to be executed. The class loader has its own synchronization built right in that make the above two points guaranteed to be threadsafe. – TlmaK0 Nov 09 '15 at 11:36
  • I may be missing something: I think that while the first thread is doing anything with the class, which may take significant time (seconds), no other thread should be able to call getInstance successfully. Are you saying that is what happens in fact? I am not understanding what happens in the second thread when getInstance is called -- does it block? – Jeff Nov 09 '15 at 12:43
  • When code calls `return Holder.INSTANCE;`, the class loader loads the class Holder. This class load is thread safe (java works this way). So any other thread trying to call `return Holder.INSTANCE` will be blocked at `return Holder` until class loader ends. During class load, all static initializers are executed, so `INSTANCE = new ...`will be executed before class loader ends and released the lock. – TlmaK0 Nov 09 '15 at 14:24