aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green mock exam 1 question 31???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green mock exam 1 question 31????" Watch "Marcus Green mock exam 1 question 31????" New topic
Author

Marcus Green mock exam 1 question 31????

Otto Deckelman
Ranch Hand

Joined: Mar 28, 2001
Posts: 30
All of Marcus' mock exams are great and his website is very helpful, I need some clarification on question#31 in his exam#1...all of the cert books and tutorials (including his own Last Minute Tutorial) teach you to call the start method in order to call the run method of a thread. The answer to Question 31 is...#3...Are there questions similar to this on the cert test that do not comply with the main stream teachings but are considered legitimate? Any tips on how to prep for this type of question? ------------------------------------------------
Exam 1, Question 31.
What will happen when you attempt to compile and run the following code?
public class Holt extends Thread{
private String sThreadName;
public static void main(String argv[]){
Holt h = new Holt();
h.go();
}
Holt(){}
Holt(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}
public void go(){
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}
public void start(){
for(int i = 0; i < 2; i ++){
System.out.println(getThreadName() +i);
try{
Thread.sleep(100);
} catch(InterruptedException e){System.out.println(e.getMessage());}
}
}
}

1) Compile time error
2) Output of first0, second0, first0, second1
3) Output of first0, first1, second0, second1
4) Runtime error

Ryan Bailey
Ranch Hand

Joined: Feb 26, 2002
Posts: 134
I would recommend you know threads inside and out for the exam. You should know by looking at this code what the answer is going to be since the code does not overwrite the run method in the thread class. Since he overwrites the start method the code is going to run just like any other code is expected to run. The second 'thread' will not execute until the first 'thread' is completely done with the overwritten start method.
Hope this helps
Regards,
Ryan


SCJP 1.4, SCWCD
Java: The power, elegance, and simplicity of a hand grenade
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
You are right. You do have to call start() to start thread execution. However, in the Holt class has a go() method that creates two new Holt() objects; first and second (these objects are themselves threads).
The go() method then calls the start method on both of these objects, thus producing the answer #3.
The reason the answers print out the way that they do is because the run() method was not overriden. If the run method had been overriden then there would be no real way to tell what the thread would print. But you do know that it would print something like #3.
[ May 16, 2002: Message edited by: Rodney Woodruff ]

Hope This Helps
Otto Deckelman
Ranch Hand

Joined: Mar 28, 2001
Posts: 30
Ryan, thanks for the reply, but still confused, if the start method is called without overriding the run method then the run method in Thread is called and nothing happens, so does that mean that you can call/override start() and implement code in the start method without overriding and implementing code in the run method. Which in turn will start a thread (via start()). So actually you do not NEED to override the run() in order to process Threads, you just need to call start() and implement code in it. I know this is not the proper approach to programming Threads (and I would not code this way myself) but I am trying to get to the root of Thread processing.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Let's clarify...
The start() method of Thread is the one that does the work to create a new thread and execute the run() method. In the above example, the start() method of Thread is overriden so running the start() method of Holt runs the overriding method. The result is that Holt doesn't run as a Thread. Even if Holt had a run() method, since the start() method of Holt doesn't set up Holt as a Thread and run the run() method, Holt would never run as a separate Thread.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Otto Deckelman
Ranch Hand

Joined: Mar 28, 2001
Posts: 30
Thomas, So this is just an example of overriding start() without instantiating a thread. Makes sense to me now.
Thanks Guys for all the help.
Ryan Bailey
Ranch Hand

Joined: Feb 26, 2002
Posts: 134
Thomas, So this is just an example of overriding start() without instantiating a thread. Makes sense to me now.
/***********************************************/
Otto, you are correct on that one. Calling the threads start() method passes the thread to the thread scheduleer which in return makes the thread eligible to run. Since the start method was overwritten it does not behave like a true thread would. I guess if you wanted to you could overwrite the start() method and run() method, make a call to super.start() in start() method and then it would run in your overwritten run() method. But why would you want to do that when you can just call the Thread's start method.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Here is the text I have given as the answer
Note that this code overrides and calls the start method. If you wished to get the output mixed you would need to override the run method but call the start method.

Perhaps I should flesh out that explanation by saying that calling start without overriding run serves no useful purpose. (Alternative/better explanations welcome)..

Thanks for asking the question Otto, I want my questions and explanations to be as good as I can.
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Marcus Green mock exam 1 question 31????