-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizationLock.java
More file actions
43 lines (38 loc) · 1021 Bytes
/
SynchronizationLock.java
File metadata and controls
43 lines (38 loc) · 1021 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
40
41
42
43
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;
class CallMe3 {
Lock lock = new ReentrantLock();
void call(String msg) {
lock.lock();
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
lock.unlock();
}
}
class Caller3 implements Runnable {
String msg;
CallMe3 target;
Thread t;
public Caller3(CallMe3 c, String s) {
target = c;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
target.call(msg);
}
}
public class SynchronizationLock {
public static void main(String[] args) {
CallMe3 target = new CallMe3();
Caller3 ob1 = new Caller3(target, "Hello");
Caller3 ob2 = new Caller3(target, "Synchronized");
Caller3 ob3 = new Caller3(target, "World");
}
}