Hi guys, I have a quiz question I hope you guys can understand
public class OrderedThread {
public static void main(String[] args) {
MBThread first,second,third;
OrderedThread orderedThread = new OrderedThread();
first = new MBThread("One",orderedThread);
second = new MBThread("Two",orderedThread);
third = new MBThread("Three",orderedThread);
second.start();
first.start();
third.start();
}
public void display(String msg) {
synchronized (msg) {
for(int i = 1;i<=20;i++) {
System.out.println("Name = " + msg);
}
}
}
}
class MBThread extends Thread {
String name;
OrderedThread orderT;
MBThread(String name, OrderedThread orderT) {
this.name = name;
this.orderT = orderT;
}
public void run () {
orderT.display(name);
}
}
The solution state that two,one and three will be printed in an indefinite order. Do you understand why is this the case? Also, I ran the code in eclipse and it was printing two,one three 20 times each in order. Lastly is there a way to print it in order? Thanks