Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question of threads from Dan's mock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question of threads from Dan Watch "question of threads from Dan New topic

question of threads from Dan's mock

Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
This is a question on Dan's new mock:

As a result, it prints "A".
But I can't figure out why!
If I don't have the class MyThread and used Thread instead, the result would be "B".
Does the run() method of a subclass of Thread have priority over the run() of the runnable passed as target to it?

Ana<p>SCJP 1.4
Sachin Tendulkar

Joined: Sep 20, 2003
Posts: 27
In the example given the run method is being over-ridden. The original run method in Thread calls the run method of the Runnable interface if the thread is passed a Runnable object.
In this case the over-riding method changes the behaviour of the run method. It simply prints "A" and never calls the run method of the Runnable interface.
As a result at run time the over-ridden run method is called and prints A.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is the run method of the Thread class.

If this method is overridden, will not be invoked. The run() method of the Runnable object will not be executed.
I agree. Here's the link:
subject: question of threads from Dan's mock
It's not a secret anymore!