-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchDemo1.java
More file actions
71 lines (62 loc) · 1.52 KB
/
SynchDemo1.java
File metadata and controls
71 lines (62 loc) · 1.52 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Threads;
class A {
static void disp() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + Thread.currentThread().getName() + " Value " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
void print() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + Thread.currentThread().getName() + " Value " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class SynchJob implements Runnable {
A obj = new A();
@Override
public void run() {
synchronized (this) {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + Thread.currentThread().getName() + " Value " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized (obj) {
obj.print();
}
synchronized (A.class) {
A.disp();
}
}
}
public class SynchDemo1 {
public static void main(String[] args) throws InterruptedException {
/*
* Job job = new Job(); Thread worker1 = new Thread(job,"ram"); Job job2
* = new Job(); Thread worker2 = new Thread(job2,"shyam"); Job job3 =
* new Job(); Thread worker3 = new Thread(job3,"sohan");
* worker1.start(); worker2.start(); worker3.start();
*/
/*
* worker1.start(); worker1.join(); worker2.start(); worker2.join();
* worker3.start();
*/
}
}