-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
55 lines (38 loc) · 1.52 KB
/
Main.java
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
55
package creational.singleton;
public class Main {
public static void main(String[] args) throws InterruptedException {
// Thread Safe singleton printer
Thread threadA = new Thread(() -> {
Printer printer = ThreadSafePrinter.getInstance();
printer.print("Thread Safe Printer A is running fine.");
});
Thread threadB = new Thread(() -> {
Printer printer = ThreadSafePrinter.getInstance();
printer.print("Thread Safe Printer B is running fine.");
});
threadA.start();
threadB.start();
threadA.join();
threadB.join();
// if you don't have multithreading Thread Unsafe singleton printer is fine
Thread threadC = new Thread(() -> {
Printer printer = ThreadUnsafePrinter.getInstance();
printer.print("Thread Unsafe Printer C is running fine.");
});
threadC.start();
threadC.join();
// but if you have multithreading Thread Unsafe singleton printer is wrong
Thread threadD = new Thread(() -> {
Printer printer = ThreadUnsafePrinter.getInstance();
printer.print("Thread Unsafe Printer D is running wrong.");
});
Thread threadE = new Thread(() -> {
Printer printer = ThreadUnsafePrinter.getInstance();
printer.print("Thread Unsafe Printer E is running wrong.");
});
threadD.start();
threadE.start();
threadD.join();
threadE.join();
}
}