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 ]
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...
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 ]
Joined: Jun 04, 2002
Huh? Does knowing what the run() method in the Thread class matter? I mean, it just gets overriden right?
Joined: Jun 18, 2002
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.
Joined: Aug 26, 2001
You could override the start method as well even though it is declared native, I guess
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
Joined: Jun 05, 2002
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 ]
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.
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
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
Never mind... [ June 20, 2002: Message edited by: Charles Earwood ]
Joined: Jun 05, 2002
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().