-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronization2.java
More file actions
42 lines (39 loc) · 997 Bytes
/
Copy pathSynchronization2.java
File metadata and controls
42 lines (39 loc) · 997 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
class Sender {
public void senderMsg(String msg) {
System.out.println("\nSending a message : " + msg);
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("sent : " + msg);
}
}
class SenderWThread extends Thread {
private String msg;
Sender sd;
SenderWThread(String msg, Sender obj) {
this.msg = msg;
sd = obj;
}
public void run() {
synchronized(sd) {
sd.senderMsg(msg);
}
}
}
public class Synchronization2 {
public static void main(String[] args) {
Sender obj = new Sender();
SenderWThread s1 = new SenderWThread("yooohooohooo", obj);
SenderWThread s2 = new SenderWThread("Welcome.", obj);
s1.start();
s2.start();
try {
s1.join();
s2.join();
} catch (Exception e) {
System.out.println(e);
}
}
}