wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Try this question I made up. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Try this question I made up." Watch "Try this question I made up." New topic
Author

Try this question I made up.

Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
The question is tough. Try it.
What will happen if you compile and run :

choose one correct answer.
1.Compile error.
2.Prints "Hello guys!"
3.Prints nothing.
4.Run error.
Note: You can trust the question since I have tested it.
Edited by Corey McGlone: Added code tags
[ June 20, 2002: Message edited by: Corey McGlone ]
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
I ran it but I don't really understand it completely. My guess is that the run() method in MyThread overrides the run() method set by the Thread constructor.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Just to clarify things. The problem here is to discover which one of the run() methods gets to be executed... Good one Alan.
PS: With your permission, I'd like to add it to my second mock exam which is in preparation...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Pierre Post
Greenhorn

Joined: Jun 18, 2002
Posts: 12
Here is the run() method of the Thread class:

So, if you override the thread's run method, the passed Runnable parameter given with the thread constructor is ignored.
Finally, the result is that nothing is printed if your code is executed.
[ June 20, 2002: Message edited by: Pierre Post ]

SCJP2
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Huh? Does knowing what the run() method in the Thread class matter? I mean, it just gets overriden right?
Pierre Post
Greenhorn

Joined: Jun 18, 2002
Posts: 12
I just wanted to show why any Runnable given with the constructor is ignored when you override the run() method.
I don't know, there could have been e.g. a test in the start() method that if a Runnable object has been given its run method will be executed or something like that. But forget this as start() is a native method.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You could override the start method as well even though it is declared native, I guess
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Yes u can override the start() methods and thus change the normal path of execution ie. start() calls run() automatically.
Remember if u overeride start of then the overridden start() will be called and the run() might not be called automatically unless you specify in your start() code.
HTH
Deepali
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Valentin,
" Just to clarify things. The problem here is to discover which one of the run() methods gets to be executed."
Your answer is ..........wrong, sorry.
Both of the run() methods get executed !
The run() method in MyThread does override the
run() method in Thread.
The following is my speculation.It can be wrong
but it is quite close to the truth.
Suppose rrr is an object reference to an object
which implements the Runnable or extends the
Thread class. When you construct another Thread object (eg.ttt) and pass rrr as the argument,
the compiler does exact work.
<<<It inserts a statement " rrr.run();" in the thread object's run() method.>>>
My code example calls super(r); so that the Thread's constructor will do the insertion.
Then I override Thread's run() method so that it doesn't call r.run();
After I call the start() method, the run() method
in MyThread object get executed. But it did nothing and then it returns.So nothing will be
printed on the screen.
Rewrite MyThread like this and you will see my
speculation is correct:

Then you will see the program print two messages.
And that proves that MyThread's run() method does execute when you call MyThread's start() method.
I have to keep a Runnable object reference
pointing to the Runnable object because I cannot
pass r to the run() method( It mustn't have any
parameters).And then I have to call the other
run() method through that object reference.
The underlying truth is interesting:
A run() method calls the other run() method.
Yes! You can use the code I made up for your
mock exam. But be careful that your explanation
to your question must be correct.
Edited by Corey McGlone: Added code tags
[ June 20, 2002: Message edited by: Corey McGlone ]
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182

I ran the above code and nothing is printed, so I think what Pierre said is true.
Francisco
[ June 20, 2002: Message edited by: Francisco A Guimaraes ]

Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Alan Chong:

<<<It inserts a statement " rrr.run();"

WHAT!?!
I really don't follow your explanation. The compiler surely isn't going to start throwing code into your program for you. Here, I've modified the original code slightly:

This program is designed to print something depending on which run method is invoked. As it is, only "T" gets printed. Therefore, the Runnable object is ignored and the run method in it is never called.
Now, had I modified the program to explcitly invoke the run method of the Runnable object (as you did in your second example), of course the run method will be invoked. But, it's not being invoked automatically by a thread, you're invoking explicitly just like any other method.
Both run methods do not get invoked. Not unless, you code it to do so. The trick to the problem, as Val stated, is to understand which run method will be invoked.
Also, on a sidenote - if you're going to post code in your message, please use code tags. It will make the code much more readable.
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Alan,
if you look what Pierre Post wrote above, you'll see how the run() method of the Thread class looks like. When you invoke start() on a thread object, it will eventually invoke the run() method of the Thread class which by default checks if there is any target and if yes invoke the run() method of the target runnable object. In your case, you have overridden the run() method in your MyThread class which removes the behavior provided by the base Thread class. So when you invoke start() on your MyThread object, only the empty run() method in MyThread will be executed and that's why nothing gets printed... Think about it... It helps to have a look at the J2SE source code sometimes
Charles Earwood
Greenhorn

Joined: Jun 04, 2002
Posts: 6
Never mind...
[ June 20, 2002: Message edited by: Charles Earwood ]
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Valentin,
I see what you mean now. The constructor of
Thread either saves the Runnable object or
set the variable target to null. Then in run()
target is checked first. This seems more efficient than to insert a line of code in run().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try this question I made up.
 
Similar Threads
thread doubt??
Thread Question
Thread Question---start()
question of threads from Dan's mock
Another Thread ques