This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A quick question on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A quick question on threads" Watch "A quick question on threads" New topic
Author

A quick question on threads

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
What's the difference between these 2 code snippets. I can't find any.



I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Nov 27, 2001
Posts: 1561
Oh yeah.. u're right man.. I need a break.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Andres
I noticed one thing that you are actually implemnting runnable and not Runnable.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Nov 27, 2001
Posts: 1561
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

Joined: Aug 05, 2002
Posts: 787
I am sorry. I introduced another typo. And did the dangerous thing: assumed.... that code will not compile....
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Nov 27, 2001
Posts: 1561
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: May 30, 2003
Posts: 443
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Nov 27, 2001
Posts: 1561
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

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Mar 05, 2003
Posts: 1391
(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

Joined: Nov 27, 2001
Posts: 1561
Thanks for the link Marlene.
[ July 26, 2003: Message edited by: Andres Gonzalez ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A quick question on threads
 
Similar Threads
Can we only start() a thread?
about synchronized keyword
Simple Doubt with Threads
Thread made out of Thread
Thread Safe Code