This discussion is archived
1 Reply Latest reply: Dec 11, 2012 10:19 PM by Kayaman RSS

What is the execution flow off the below program??

979363 Newbie
Currently Being Moderated
When I tried to execute the below program ,
class A extends Thread{
int count = 0;
public void run(){
System.out.println("run");
synchronized (this) {
for(int i =0; i < 50 ; i++){
count = count + i;
}
notify();
}
}
}
public class q7146{
public static void main(String argv[]) {
A a = new A();
a.start();
//System.out.println("started");
synchronized (a) {
System.out.println("waiting");
     try{
     a.wait();
     }catch(InterruptedException e){
}
System.out.println(a.count);
}
}
}


The output was waiting run 1225
When I uncomment the line System.out.println("started");
The output is started run waiting 1225

One single line in has introduced change in the odrer of priniting 'waiting' and 'run'.

So what is the low of execution in the program and waht is the logic behind this. Please somebody explain this.

Thankx for the help in advance.
  • 1. Re: What is the execution flow off the below program??
    Kayaman Guru
    Currently Being Moderated
    First of all, put your code between
     tags so it formats correctly.
    
    Secondly, try running the program a few times and see whether you get consistent results.
    
    Thirdly, do your own homework.                                                                                                                                                                                                                                                                                                                                                                                                                

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points