• 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

Threads

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question taken from JQuest mock exam.
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 ?
a. The doStuff method will be called repeatedly.
b. The doStuff method will never be executed.
c. The doStuff method will execute atleast one time.
d. The statement in line 10 will never be reached.
According to me 'a' is the right answer. But the correct answers are 'b' and 'd'.
Can anybody explain me why 'b' and 'd' are the correct answers.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
start() should be called after creating of thread, run() could not be executed and that a reason for doStuff will never be executed.
it is example of code that never ensured that thread will be created at all, actually it could not compile even doStuff() is defined.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinay,
You are calling the start() of Whiffler Object and not of Thread Object myT. This is necessory since the start() defined in Thread class
actually creates a separate execution path(nothing but a thread) and calls run().
Regards
-------
vadiraj
------------------
*************************
There's a lot of I in J.
*************************
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this code does compile. My understanding of the explanation is there is no thread ever started on this class. Hence the run method nerver called and no output either.
 
reply
    Bookmark Topic Watch Topic
  • New Topic