-
Notifications
You must be signed in to change notification settings - Fork 1
/
SyncMutualExclusive.java
95 lines (74 loc) · 2.78 KB
/
SyncMutualExclusive.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Mutual Exclusive Synchronization :
1. Mutual exclusion is a property of process synchronization which states that “no two processes can exist in the critical section at any given point of time
2. synchronization can be done only for same shared resource. (same object or same static function or block of code)
*/
class Stats {
synchronized static void details() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getId() + " : " + Thread.currentThread().getName());
}
}
synchronized void moreDetails(){
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getId() + " : " + Thread.currentThread().getPriority() + " : " + Thread.currentThread().getName());
}
}
void trashWithDetails(){
for (int i = 0; i < 40; i++)
System.out.print("*");
System.out.println();
// critical section starts
synchronized(this) // This -> same object Table-td
{
for (int i = 0; i < 5; i++)
System.out.println("☠️ Critical Section of "+ Thread.currentThread().getName());
}
// critical section Ends
for (int i = 0; i < 40; i++)
System.out.print("-");
System.out.println();
}
static void staticDetails() {
for (int i = 0; i < 40; i++)
System.out.print("*");
System.out.println();
synchronized(Stats.class) // critical section in static function
{
for (int i = 0; i < 5; i++)
System.out.println("☠️ Critical Section of "+ Thread.currentThread().getName());
}
for (int i = 0; i < 40; i++)
System.out.print("-");
System.out.println();
}
}
class Thread0 extends Thread {
Stats td ;
public Thread0(String string, Stats td) {
super(string);
this.td =td;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Started....");
Stats.details(); // sync static method
td.moreDetails(); // sync instance method
td.trashWithDetails(); // sync block in method
System.out.println(Thread.currentThread().getName()+" Finished....");
/*
sync is bet. same critical section of all threads
but not in different critical sections of same or different threads
*/
}
}
public class SyncMutualExclusive {
public static void main(String[] args)
{
Stats td = new Stats();
Thread thread1 = new Thread0("Thread 1",td);
Thread thread2 = new Thread0("Thread 2",td);
thread1.start();
thread2.start();
}
}