aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Runnable vs Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Runnable vs Thread" Watch "Runnable vs Thread" New topic
Author

Runnable vs Thread

Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56

�Another reason to use the Runnable interface is that you may need to run an object multiple times�However, you can create as many threads as you want by using a Runnable instance.�
From Mike Meyers� certification Passport-Java 2

This is quoted from a paragraph that is telling me the difference between java.lang.Thread and java.lang.Runnable. I understand the deal with regular class versus interface class, but what is this implication that extending Thread would only allow me to create on thread while implementing Runnable would let me create multiple threads? And in case this was not answered, what is the difference between java.lang.Thread and java.lang.Runnable besides that one is interface?


Let us be showered in the light of confusion!
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
I thought in both cases, you can create multiple threads.
The most obvious reason for preferring to implement the Runnable interface rather than extend the Thread class, is that by implementing the Runnable interface, you still have the option of extending another class, whereas if you extended the Thread class to create threads then you can't extend any other class.


Amir
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
When you extend Thread, each instance you create is like creating a new "processor" only for that thread. In the other hand, when you implement the Runnable interface it�s like you�re using one "processor" for all your threads. That�s the explanation I remember from the R&H book. Anyone correct me if i�m wrong.
Hope that helps,
Francisco


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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Well, I don't know if I exactly agree with what Mike says in the first place, but here are is the difference between Thread and Runnable.
1. Thread is a class. Runnable is an interface.
2. Thread implements Runnable.
3. In order to create a new thread, you must, at some point, use the syntax "new Thread." Making a Runnable object is optional as a Thread [i]is a[/a] Runnable object.
I hope that helps,
Corey


SCJP Tipline, etc.
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
Originally posted by Francisco A Guimaraes:
When you extend Thread, each instance you create is like creating a new "processor" only for that thread. In the other hand, when you implement the Runnable interface it�s like you�re using one "processor" for all your threads. That�s the explanation I remember from the R&H book. Anyone correct me if i�m wrong.
Hope that helps,
Francisco


Huh???

Please explain it further, because I don't understand what do you mean by processor.
Corey, I gather as far as practical purpose there is no difference between Thread and Runnable besides one being interface.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

When you extend Thread, each instance you create is like creating a new "processor" only for that thread. In the other hand, when you implement the Runnable interface it�s like you�re using one "processor" for all your threads.

I think this remark hit the nail on its head. Let's try it with code:

Each Strider object is running on its own, well, thread, while two Threads are accessing the single Runner object in a multi-threaded manner.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Heh heh. Forgot to add,

at the top of the code.
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
After I run the program, it seems to me that If I use Runnable as a target for threads, what I have are multiple threads that can use the same object, namely whatever I got that implements Runnable, thus the deal with running one object multiple time. With Thread, I'll get multiple threads each running its own copy the object.
Is that about right?
Thanks in advance, I have trouble on the finer details of threads...guess that makes me a single-minded person get it? not multiple thread, so single thread and that means single-minded?...(humph, dry joke...study too hard?)
[ June 26, 2002: Message edited by: Chung Huang ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

You might want to go and read the following series of articles on threads in JavaWorld:
Achieve strong performance with threads
Programming Java threads in the real world


Junilu - [How to Ask Questions] [How to Answer Questions]
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi all,
Everybody is trying to complicate the simple nature of Thread. Suppose r is an object that
implements Runnable and provide the run() method.
Thread t = new Thread(r);
t.start();
Then t'run() method will call r's run() method.
And when r's run() returns, execution go back to
t'run() method. It's just a simple delegation !
The Thread class is the only necessary thing to
create a thread. Runnable is just a MARK which tells one of Thread's constructor that:
You can delegate your program flow to the
object that has the MARK of Runnable.
A Runnable object's run() method just gets
wedged into the run() method in a Thread object.
Only a Thread object can make up a thread because
only a Thread object has a start() method that can prepare for itself to run as a thread.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

After I run the program, it seems to me that If I use Runnable as a target for threads, what I have are multiple threads that can use the same object, namely whatever I got that implements Runnable, thus the deal with running one object multiple time. With Thread, I'll get multiple threads each running its own copy the object.
Is that about right?

Yes.

Thanks in advance, I have trouble on the finer details of threads...

Personally, so do I . IMHO I think threads are the trickiest part of the exam...
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Chung Huang:
After I run the program, it seems to me that If I use Runnable as a target for threads, what I have are multiple threads that can use the same object, namely whatever I got that implements Runnable, thus the deal with running one object multiple time. With Thread, I'll get multiple threads each running its own copy the object.
Is that about right?

Well, it can be. Don't forget that a Thread implements the Runnable interface. Therefore, the following is legal:

In this case, we've used a single Thread object to share an object between two threads. I'm not sure why you'd want to do such a thing, but it is possible.
It's difficult to separate Thread and Runnable becuase a Thread "is a" Runnable. The converse is not true, but the Runnable interface is seldom (if ever) used without a thread.
Corey
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

new Thread((Runnable)mt).start();
new Thread((Runnable)mt).start();

Strictly speaking, the cast is unnecessary, since, as you've said, Thread implements Runnable. If class X extends Thread, then X is Runnable too.
Anyway, I really fail to see the point of using the Thread(Runnable r) constructor if you're extending Thread already, as the run method is already overriden. The example above is certainly possible, but unique.
As I see it, the usual course of action is:
class X extends Thread, overrides run() and calls Thread(), in which case each X object goes on its merry way...
OR, class X implements Runnable, implements run() and calls Thread(Runnable r), in which case each X object is being accessed by (possibly) multiple threads.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Anthony Villanueva:

OR, class X implements Runnable, implements run() and calls Thread(Runnable r), in which case each X object is being accessed by (possibly) multiple threads.

But, quite often, you don't want multiple threads sharing the same object. It is very possible to use multiple Runnable objects, like this:

Corey
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Ah, now that is certainly true...
Thanks for pointing it out
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Runnable vs Thread