• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wait() doubt

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from the examulator mock exam

public class Agg{
public static void main(String argv[]){
Agg a = new Agg();
a.go();
}
public void go(){
DSRoss ds1 = new DSRoss("one");
ds1.start();
}
}

class DSRoss extends Thread{
private String sTname="";
DSRoss(String s){
sTname = s;
}
public void run(){
notwait();
System.out.println("finished");
}
public void notwait(){
while(true){
try{
System.out.println("waiting");
wait();
}catch(InterruptedException ie){}
System.out.println(sTname);
notifyAll();
}
}

}



1 It will cause a compile time error
2 Compilation and output of "waiting"
3 Compilation and output of "waiting" followed by "finished"
4 Runtime error, an exception will be thrown

I choosed 1 (compile time error) because the wait is done without synchronize, but in the answer it's said the correct response is 4 (Runtime error) for the same reason (wait without synchronize).

When I try to compile this code, it shows a compile error (as I I reponded). So is the java.lang.IllegalMonitorStateException compile time or a runtime error ?
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the code -> compiles without problems, but throws an llegalMonitorStateException. If you look in its API description, you see, that it's derived from runtime exception, so it is an unchecked exception.
[ July 19, 2008: Message edited by: Ralph Jaus ]
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic