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

Exam Cram Q about Threads

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class whiffler extends object implements Runnable
{
Thread myT;
public void start()
{
myT = new Thread(this);
}
public void run()
{
while(true)
{
dostuff();
}
System.out.println("Exiting run");
}
//more class code follows...............
Assume that the rest of the class defines dostuff() and so on and class compiles without error.Also assume that a Java application creates a hiffler object and calls the Whiffler start method , that no other calls to Whiffler methods are made and the the thread in this object is the only one the application creates ..
a.dostuff () called repeatedly
b.dostuff() will never be excecuted
c. dostuff() will execute atleast once
d.statement in System.out.println() will never be reached
the answer is b & d
If some one can explain
This is from Exam cram page 182.
Wali
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wali,
One is tricky; the other is a stupid programmer trick.
(b) Although the Whiffler class has a start method, it is the Thread's start method that needs to be called to start the thread (put it into the ready state).
The constructor is fine but you need a line following it like:
myT.start();
(d) The while loop's conditional statement is filled with a boolean literal...it will never be not true (false) and therefore exit the loop and execute the line "System.out.println("Exiting run");" (it's an endless loop).
Actually the code won't compile without error because the compiler is smart enough to know that the statement is unreachable...but that is beside the point...the question says "assume it compiles without error."
I personally don't think it's fair to say "assume" the code compiles without error when in fact it won't and then ask a question about the assumed away compile-time error.
Here's an example:

Best regards,
Steve Butcher
exceptionraised@aol.com
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, the intent of this question was to point out the requirement for calling the start method belonging to the Thread attached to the Runnable object. Maybe I should have said:
while( someMethodReturningTrue() ){
doStuff();
}
but I was trying to keep the question compact to avoid distracting the user from the critical code.
Bill
 
sgwbutcher
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
I just get nitpicky as my stress level rises...some of the mock exam questions (not yours actually) are so convoluted I almost wish it were two or three pieces of code, a compiler without messages and say 3-4 hours to debug the code...essentially a computer programming essay.
Anyway, your book is great and we'll see if I did it justice on Wednesday...if not, I'll just get back on the horse.
Cheers,
Steve
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic