Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Runnable vs Thread

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


�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?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh heh. Forgot to add,

at the top of the code.
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 17665
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, now that is certainly true...
Thanks for pointing it out
 
reply
    Bookmark Topic Watch Topic
  • New Topic