This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Questions from JQ

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from JQuest.
Here is part of the code for a class which implements the Runnable interface.
1. public class Whiffler extends Object implements Runnable {
2. Thread myT ;
3. public void start(){
4. myT = new Thread( this );
5. }
6. public void run(){
7. while( true ){
8. doStuff();
9. }
10. System.out.println("Exiting run");
11. }
12. // more class code
Assume that the rest of the class defines doStuff, etc and that the class compiles without error. Also assume that a Java application creates a Whiffler object and calls the Whiffler start method, that no other direct calls to Whiffler methods are made an that the Thread in this object is the only one the application creates. Which of the following are correct statements ? [Select two valid answers]
1.doStuff() is never executed 2. line 10 will never be reached
Why these two answers??
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Because start() is never called for the thread-object (myT), thus run() will never be eexcuted.
2. Assuming that we eventually call run(), the while-loop on line 7 has no way of exiting (the constant true will never change). Thus all the code after will be considered unrechable (which actually is a compiler error i Java, so the statement "assume that the program compiles" is wrong).
/Mike
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mikael, I think the text under the code in Kevin's post says that the Whiffler start method is indeed called. However, because Whiffler immplements Runnable rather than extending Thread, the start() method has no function other than what is defined in the class, which is simply to create a new Thread (myT). Creating a Thread does not cause it to run, and nothing calls the run() method defined in the Whiffler class.
I agree with Mikael on the second point -- the code should not compile due to the Unreachable Statement in line 10.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mikael Jonasson:
1. Because start() is never called for the thread-object (myT), thus run() will never be eexcuted.
2. Assuming that we eventually call run(), the while-loop on line 7 has no way of exiting (the constant true will never change). Thus all the code after will be considered unrechable (which actually is a compiler error i Java, so the statement "assume that the program compiles" is wrong).
/Mike


I agree with you Mike that start method is never called on THREAD myT thought start method of the whiffler object is definetely caled but that will not execute run method.
However if you add myT.start() then run method is executed but again you will get statement not reached and it should not compile due to infinite loop of while(true)
Jyotsna
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jyotsna Umesh:
However if you add myT.start() then run method is executed but again you will get statement not reached and it should not compile due to infinite loop of while(true)
Jyotsna


Mike & Jyotsna,
I tried in my Laptop Windows98 SE using TestPad editor, "while (true)" DOES NOT cause compilation problem, but it does create
infinite loop when run. Regards,
Daniel
 
Daniel Wu
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Appleton:
Mikael, I think the text under the code in Kevin's post says that the Whiffler start method is indeed called. However, because Whiffler immplements Runnable rather than extending Thread, the start() method has no function other than what is defined in the class, which is simply to create a new Thread (myT). Creating a Thread does not cause it to run, and nothing calls the run() method defined in the Whiffler class.
I agree with Mikael on the second point -- the code should not compile due to the Unreachable Statement in line 10.


Scott,
I agree with the two points you made. Just wondering why we couldn't insert a standalone "System.out.println("Stuff...")" in a place outside of main. My experience is that ONLY the println statement is placed in main will it work.
System.out.println("Stuff...")" SIMPLY CAUSE NOT TO COMPILE IF LEFT OUTSIDE OF MAIN. Do you guys agree with that? Some reasoning? Thanks.
Daniel
 
He does not suffer fools gladly. But this tiny ad does:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic