File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Query regarding instantiation of THREAD using Implements Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Query regarding instantiation of THREAD using Implements Runnable" Watch "Query regarding instantiation of THREAD using Implements Runnable" New topic
Author

Query regarding instantiation of THREAD using Implements Runnable

isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Hi All,

when a class implements a Thread eg:



When we create an object of this class in other class



This object exposes run method which we have overrided in MyThread class. Why do we need to Create Thread Class instance separetly and then assign mt to that thread as

Thread t= new Thread(mt);

Why we need separate handler?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

First of all, you can't use "implements Thread". Thread is a class - if you want to inherit from it you use "extends". You may be getting it confused with Runnable, which is an interface.

Secondly, to answer your question: you don't. If you've created a Thread, or anything that inherits from Thread, you can just start it. If you've created a Runnable object, you have to pass that to a Thread. Since Thread implements Runnable, that means you can pass a Thread to another Thread - but just because you can doesn't mean you should.
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
Hi ,

Yeah ,have mistaken Runnable with Thread..

I created an instance of Class MyThread which Implements Runnable.
have written implementation in run().
Now if i want to start this thread, i can't because Runnable interface has only one method RUN().So to bring other methods of start and stopping a thread, i need to pass this object to Thread Class.

Is this explanation write?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Query regarding instantiation of THREAD using Implements Runnable