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