Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A quick question on threads

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between these 2 code snippets. I can't find any.

 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
What exactly do you mean by difference.
The first one implements Runnable and therefore it's necessary for it to provide the run method and it's not so with the later.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yeah.. u're right man.. I need a break.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
I noticed one thing that you are actually implemnting runnable and not Runnable.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note following points:
1. Thread already implements Runnable.
2. Seems like that there is a typo in code posted (runnable instead of Runnable).
3. If you take care of typo, code will not compile. I would say because same interface can not be reimplemented.
Thanks
Barkat
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat
Please note following points:
1. Thread already implements Runnable.
1. Yes, they do. As Thread is a non-abstract class it has to implement the run method and it does so.
2. Seems like that there is a typo in code posted (runnable instead of Runnable).
2. Yes again, looks a like a typo to me as well.
3. If you take care of typo, code will not compile. I would say because same interface can not be reimplemented.
3. No, did you try to compile the program(with the typo corrected). Why can't you reimplement an interface.
[ July 25, 2003: Message edited by: Anupam Sinha ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup, there was a typo, sorry about that.

If you take care of typo, code will not compile.

The code does compile.

I thought the same when I first saw this code. But class Thread has a run() method that does nothing and returns.
So, if you have this

The code compiles and runs printing nothing.
[ July 25, 2003: Message edited by: Andres Gonzalez ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry. I introduced another typo. And did the dangerous thing: assumed.... that code will not compile....
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is a nit, but the run method of Thread actually does do something.

That may not look like much. But a while ago for some reason I cannot remember, target was set by the constructor and then later it was reset to null before the thread had a chance to run. I'll have to find that post.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting Marlene, I wonder where you got that piece of code in the run() method. My assumption was based in JDK 1.4.1 API doc

public void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
The code is from the Thread's class source code. Secondly both the code(Marlene's) and the statement(your's) mean the same. The target is actually an object of type Runnable. If target is not null then you can expect the thread to run the run() method that is the Runnable's implementation run method, if properly implemnted. Otherwise this method would do nothing and exit.
[ July 25, 2003: Message edited by: Anupam Sinha ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So going back to the original question, what is the difference between the 2 pieces of code?
It looks like there is none. The code that implements Runnable does not need to implement anything. The Thread class already implements Runnable so the contract had been fulfilled.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alton
Yes Alton the contract has been fulfilled for the class becasue the run() method of the Thread class has been inherited by the Test2 class.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupam Sinha:
Hi Alton
Yes Alton the contract has been fulfilled for the class becasue the run() method of the Thread class has been inherited by the Test2 class.


Exactly. I think it's a bit tricky if you see it the very first time. You might think that there's a compilation error because class Thread does not implement the run() method, but indeed class Thread has a run() method itself.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andres
Yes that's exactly what I am saying. I actually thought that there would be a compilation error had your class ATest been compiled. I didn't realised that it contains the inherited run() method of the Thread class. My first post in this thread reflects this.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I apologize for interrupting (again) your topic of discussion with asides, but I found the post where I needed to know that run() does something. It a curiosity, a pathological situation. It depends on Thread implementing Runnable, so it might be of interest to you. Here)
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Marlene.
[ July 26, 2003: Message edited by: Andres Gonzalez ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic