Question: Review the following code examples and improve them (You should keep and improve the multithreading parts). Once the code files have been fixed, make them
Review the following code examples and improve them (You should keep and improve the multithreading parts). Once the code files have been fixed, make them flexible in such a way that the words are provided by a user. In other words, the program asks a user to enter three sets of words which are used to list the words.
public class Called {
void call(String msg) {
System.out.print(" [" + msg);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
public class Caller implements Runnable {
String msg;
Called target;
Thread trd;
public Caller(Called tgt, String s) {
target = tgt;
msg = s;
trd = new Thread(this);
}
public void run() {
target.call(msg);
}
}
public class TestSynchronization {
public static void main(String[] args) {
Called target = new Called();
Caller caller01 = new Caller(target, "CIS 314");
Caller caller02 = new Caller(target, "Advanced Programming");
Caller caller03 = new Caller(target, "Java");
caller01.trd.start();
caller02.trd.start();
caller03.trd.start();
try {
caller01.trd.join();
caller02.trd.join();
caller03.trd.join();
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
The output is supposed to be the following:
[CIS 314]
[Advanced Programming]
[Java]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
