-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriver.java
More file actions
54 lines (39 loc) · 1.07 KB
/
Driver.java
File metadata and controls
54 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package singleton;
/**
* Driver class (Main)
* Calls the test method
* @author jamaldev
* @created 9/10/17
*/
public class Driver {
public static void main(String[] args) {
//Test First Usage (Lazy Singleton)
testLazySingleton();
//Test Second Usage (Thread Safe)
testThreadSafeSingleton();
}
/**
* Test LazySingleton. create 2 instances and print out the letters
*/
private static void testLazySingleton() {
LazySingleton inst1 = LazySingleton.getInstance();
LazySingleton inst2 = LazySingleton.getInstance();
System.out.println(inst1.hashCode());
inst1.printData();
System.out.println(inst2.hashCode());
inst2.printData();
}
private static void testThreadSafeSingleton() {
new DataPrinter().start();
new DataPrinter().start();
}
}
/**
* Dummy Thread that calls the getInstance and printData method
*/
class DataPrinter extends Thread {
@Override
public void run() {
ThreadSafeSingleton.getInstance().printData();
}
}