File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
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


Sean <br />SCJP2, SCJP2p1.4, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks guys,
that's helped me out lots.
Cheers

Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
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 !!


Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
'Cav'
PROPER NAMES ARE NOW REQUIRED!!
Read this post for more details.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Brian, Cavanagh
Ranch Hand

Joined: Oct 30, 2000
Posts: 70
done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question