Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?
public class BriarMill extends Thread{
public static void main(String argv[]){
BriarMill bm = new BriarMill();
bm.go();
}
public void go(){
start();
}
public void payBill(){
try{
wait();
}catch(InterruptedException ie){}
System.out.println("done");

}
public void run(){
payBill();
notifyAll();
}

}



1) Compilation but runtime error.
2) Compilation and output of done at runtime
3) Compilation but no output at runtime, the program will run and wait with no output.
4) Compile time error

According to me the answer wuld be 4,but stated wrong plz guide me to the
Appropriate answer.
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess the problem may bewith wait() and notifyall() methomk
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code called bm.wait() when it didn't own the lock on bm.

This throws an IllegalMonitorStateException.
 
nagaraj reddy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divakar you must use wait nad notify methods in Synchronized method or synchronized block only otherwise it will be a error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic