-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedBlocks
More file actions
61 lines (43 loc) · 1.01 KB
/
SynchronizedBlocks
File metadata and controls
61 lines (43 loc) · 1.01 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
package threads;
public class SynchronizedBlock {
public void wish(String name) {
//synchronized(this) {
synchronized(SynchronizedBlock.class) {
// synchronized(b) {
for(int i=0;i<5;i++) {
System.out.print("Good Morning :");
try {
Thread.sleep(2000);
}
catch(InterruptedException e) {
System.out.println(e);
}
System.out.println(name);
}
}
}
}
package threads;
public class MyRunnable1 extends Thread {
String name;
SynchronizedBlock b;
public MyRunnable1(String name, SynchronizedBlock b) {
this.name = name;
this.b =b;
}
public void run() {
b.wish(name);
}
}
package threads;
public class MainThread {
public static void main(String[] args) {
SynchronizedBlock b = new SynchronizedBlock();
// SynchronizedBlock b1 = new SynchronizedBlock();
MyRunnable1 t1 = new MyRunnable1("Alekha chowdary",b);
MyRunnable1 t2 = new MyRunnable1("Srilakshmi",b);
// MyRunnable1 t3 = new MyRunnable1("Hanumantharao",b1);
t1.start();
t2.start();
}
}