• 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

Marcus Green mock exam 1 question 31????

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Otto Deckelman
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Otto Deckelman
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic