Singleton Flashcards

1
Q

Singleton pattern

A

used to create one and only instance of a class and a global point of access to it exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

examples of objects that should only have a single instance

A

Caches, thread pools, registries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how do we ensure that only one object ever gets created?

A

to make the constructor private of the class we intend to define as singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what happens when we make the constructor private of the class?

A

only the members of the class can access the private constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Two ways that may fix a race condition, under Multithreading conditions?

A
  • add synchronized to the getInstance() method.

// Create a static method for object creation
synchronized public static AirforceOne getInstance() {
// Only instantiate the object when needed.
if (onlyInstance == null) {
onlyInstance = new AirforceOne();
}
return onlyInstance;
}

  • undertake static initialization of the instance, which is guaranteed to be thread-safe// The sole instance of the class
    private static AirforceOne onlyInstance = new AirforceOne();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the problems with “add synchronized to the getInstance() method” and “undertake static initialization of the instance” that may fix a race condition when the applications be under Multithreading condition?

A

synchronization is expensive and static initialization creates the object even if it’s not used in a particular run of the application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is “double-checked locking” when the applications be under Multithreading condition?

A

synchronize only when the object is created for the first time and if its already created, then we don’t attempt to synchronize the accessing threads.

“double-checked locking

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How “double-checked locking” is implemented in Java?

A

// The sole instance of the class. Note its marked volatile
private volatile static AirforceOneWithDoubleCheckedLocking onlyInstance;

// Create a static method for object creation
synchronized public static AirforceOneWithDoubleCheckedLocking getInstance() {

    // Only instantiate the object when needed.
    if (onlyInstance == null) {
        // Note how we are synchronizing on the class object
        synchronized (AirforceOneWithDoubleCheckedLocking.class) {
            if (onlyInstance == null) {
                onlyInstance = new AirforceOneWithDoubleCheckedLocking();
            }
        }
    }

    return onlyInstance;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JVM volatile implementation for Java versions 1.4

A

will not work correctly for double checked locking

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The double checked locking is now considered

A

an antipattern and its utility has largely passed away as JVM startup times have sped up over the years.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

In the Java API we have the following singletons:

A

java.lang.Runtime
java.awt.Desktop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly