• 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

Is "while(true)" valid?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code snippet given below is taken from Marcus Green's Mock Exam 1

It was the healthy exam

But the answer(output) for this question is 4 out of following oprions

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

My answer is 1 as code below contains "while(true)".

Please guide am I right or wrong?


<code>
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();
}
}
}
</code>
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while (true) is valid syntax because true is a special boolean constant.

The reason for the runtime error is that you are calling the wait() method, but it's not called inside a synchronized block or method.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
[QB]while (true) is valid syntax because true is a special boolean constant.QB]



No issue there. But why is this not defined as a compile-time error?
The compiler should have no difficulty in seeing that the wait() is
called incorrectly!? Or is that being to pessimistic?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about if that wait() is called in a method called by a method called by...
The compiler ain't going to catch that.
 
Hans Beck�rus
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right. That is a possible scenario.
The compiler would be able to catch this if it was within the same
source file, but otherwise would fail. Better to have the same
behavour in both cases instead of mixing the two!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since wait(0 is inside nosynchronized notwait method and while loop it causes compiler error
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic