Imagine you have created a class called Mouth. Its sole purpose is to say one true and correct statement.
public class Mouth {
public void first() {
System.out.print("I ");
}
public void second() {
System.out.print("love ");
}
public void third() {
System.out.print("programming");
}
}
The same instance of Mouth will be passed to three different threads. Thread A will call first(), thread B will call second(), and thread C will call third(). Design a mechanism and modify the program to ensure that second() is executed after first(), and third() is executed after second().
The input format is space-separated letters A, B, and C, like:
input = A C B
There are three threads being fired asynchronously. The input A C B means thread A calls first(), then thread C calls third(), and lastly thread B calls second(). However, the program should still print "I love programming!", which is the correct output.