• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query regarding instantiation of THREAD using Implements Runnable

 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic