• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which run() method will run?  RSS feed

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question from Dan Chisholm's mock exam:



According to Dan, this code snippet causes the run() method in class A to run, not class B. But why? I've read his explanation, but I still don't understand. If you pass a Runnable object to a Thread constructor, then shouldn't the Thread object invoke the run() method of the Runnable object?

PS: I tested this code snippet, and true enough, Dan is correct.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Follow these steps:
Call start() on A
start() call run() on A
-- run() has been overriden by class A
run() prints A

Now other steps
supose Thread t = new Thread(new B());
Call start() on t
start() call run() on t
run() calls b.run() method
b.run() prints B

The implementation of the run method of the Thread class is the following:
public void run() {
if (target != null) {
target.run();
}
}
where target is the runnable object you passed in the construtor
if you override you don't get target run

I hope I helped
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can be rewritten as

[ January 24, 2005: Message edited by: Richard Vagner ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Because the run() method in A has no call to super.run(), a check is never made to see if it has a Runnable object to call.

If you add a call to super.run() in class A's run() method, you'll see B printed first. This is because the default implementation of run() is called first, which checks if it has a reference to a Runnable, and calls it if it does.

Cheers
Richard

p.s, I would have said that it prints B too!
[ January 25, 2005: Message edited by: Richard Levy ]
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by rogel garcia:
Follow these steps:
Call start() on A
start() call run() on A
-- run() has been overriden by class A
run() prints A

Now other steps
supose Thread t = new Thread(new B());
Call start() on t
start() call run() on t
run() calls b.run() method
b.run() prints B

The implementation of the run method of the Thread class is the following:
public void run() {
if (target != null) {
target.run();
}
}
where target is the runnable object you passed in the construtor
if you override you don't get target run

I hope I helped



Cool. Thank you very much. Now I understand. Due to the fact that the run() method in the Thread class has been overriden by Class A with an empty method, so the Runnable run() method has no chance to run. I hope I'm right.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!