Why does the thread 3 execute before thread 2 though iam invoking it before thread 3?
if i am right.. after invoking thread 1 the printmsg1 enters into the synchronized state. thread 2 is put in the stack then thread 3 is put in the stack. so after the thread 1 has finished thread 3 is poped and run. finally thread 2.
if this is the case how do i make them run in the order the threads are invoked? ie after thread 1 releases the block thread 2 must acquire the lock then thread 3.
class printmsg{
synchronized void printmsg1(String data, int timer){
System.out.println(data);
try {
Thread.sleep(timer);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
class Welcome implements Runnable{
Thread t1;
String data;
int timer;
boolean flag;
printmsg pmsg;
Welcome(printmsg pmsg, String data, String tName, int time){
t1 = new Thread(this, tName);
this.data = data;
timer = time;
this.pmsg = pmsg;
t1.start();
}
public void run() {
pmsg.printmsg1(data, timer);
}
}
public class Main {
public static void main(String[] args) {
printmsg pmsg = new printmsg();
new Welcome(pmsg, "Thread 1", "Hellowa", 1000);
new Welcome(pmsg, "Thread 2", "Hellowb", 1000);
new Welcome(pmsg, "Thread 3", "Hellowc", 1000);
}
}