Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

Thread Question

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone have a look at this mock question, and tell me why the answers are b and d. I got d, but I do not understand the while(true) condition and therefore why b is a correct answer??? Any words of wisdom out there?
1. public class aThread 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 aThread object and calls the aThread start method, that no other direct calls to aThread methods are made an that the Thread in this object is the only one the application creates. Which of the following are correct statements ?
a. The doStuff method will be called repeatedly
b. The doStuff method will never be executed
c. The doStuff method will execute at least one time
d. The statement at line 10 will never be reached
Thx in advance guys
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when aThread's start() method is called we get an instantiation of Thread with aThread passed in as an arguement.
we have overriden a start() method but it start() doesn't call run() .... that is why run() will never be excuted.
and if you implement start() so that it has the following two lines.
myT = new Thread(this);
myT.start();
then it will go into infinite loop..
but without calling the start method of myT thread.
it won't call aThread's run() at all .. so b) is right..
hope this helps..
Sean
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
that's helped me out lots.
Cheers

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cav.For a Thread to execute , u have to register it with a piece of code called the THREAD SCHEDULER.This puts the Thread in the READY state.This is done by calling the start() method of the Thread class.The run() method is called when the Thread Scheduler brings the Thread into Running state.So with this knowledge let's look at the options one again...
a. The doStuff method will be called repeatedly - This will be the case if u call the start() method for the newly created thread in ur program.That is , u say
Thread t = new Thread(new aThread());
t.start();
Since this is not the case , option a) is FALSE.
b. The doStuff method will never be executed - TRUE.As explained above , u have to call the start() method of the THREAD class.What u have done in ur code is to DEFINE a method start() in ur aThread class , and not OVERRIDE the start() method in the Thread class.Also , u will have to call myT.start() in order to register ur thread.Since this is not the case , run() will not be called and doStuff() will never be executed.
c. The doStuff method will execute at least one time - FALSE.As explained above.
d. The statement at line 10 will never be reached - TRUE.Since run() is never going to be called , so any code inside run() will never be executed.


------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Cav'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
done
 
They worship nothing. They say it's because nothing is worth fighting for. Like this 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