-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading7.java
More file actions
45 lines (39 loc) · 1.24 KB
/
MultiThreading7.java
File metadata and controls
45 lines (39 loc) · 1.24 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
/**
* Created by Admin on 15-04-2017.
*/
public class MultiThreading7 {
//An example of race condition in which all the threads are trying to complete their output
//since none of them waits for any other, they all race to the end and the outputs gets mixed
//up. using 'synchronized' in Callme.callMe can stop this problem
static class Callme{
synchronized void callMe(String msg){
char[] chars = msg.toCharArray();
for (char c : chars) {
System.out.print(c);
}
}
}
static class Caller implements Runnable{
String msg;
Callme callme;
Thread t;
Caller(Callme callme, String msg){
this.msg=msg;
t=new Thread(this);
this.callme=callme;
}
@Override
public void run() {
callme.callMe(msg);
}
}
public static void main(String[] args){
Callme callme=new Callme();
Caller caller1=new Caller(callme, "ABCD");
Caller caller2=new Caller(callme, "EFGH");
Caller caller3=new Caller(callme, "IJKL");
caller1.t.start();
caller2.t.start();
caller3.t.start();
}
}