-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizedBlock.java
More file actions
39 lines (35 loc) · 886 Bytes
/
SynchronizedBlock.java
File metadata and controls
39 lines (35 loc) · 886 Bytes
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
class CallMe2 {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller2 implements Runnable {
String msg;
CallMe2 target;
Thread t;
public Caller2(CallMe2 c, String s) {
target = c;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
synchronized (target) {
target.call(msg);
}
}
}
public class SynchronizedBlock {
public static void main(String[] args) {
CallMe2 target = new CallMe2();
Caller2 ob1 = new Caller2(target, "Hello");
Caller2 ob2 = new Caller2(target, "Synchronized");
Caller2 ob3 = new Caller2(target, "World");
}
}