Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Deciding to Use the Runnable Interface/Thread  RSS feed

 
Ramneek Handa
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body,
There are two ways to provide run method for a java thread
1) Subclass the Thread class defined in the java.lang package and override the run method.
2) Provide a class that implements the Runnable interface (also defined in the java.lang package) and therefore implements the run method. In this case, a Runnable object provides the run method to the thread.
Now How to choose either of these options over the other. although there is a rule of thumb for that.
Rule of Thumb: If your class must subclass some other class (the most common example being Applet), you should use Runnable as described in option #2.
a) Can anybody give me one good reason apart from the above to choose either of them.
b) Can we use threads in an applet without implementing Runnable interface( by creating an instance of another class which extends Thread class or some other way).
I thanks in adv. and sorry if I am asking some nonsense but these doubts haunting me for long.
Waiting for some good resons to choose between Thread and Runnable....
 
Angela Lamb
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason I can think of to use Thread rather than Runnable would be if you need to override one of the other thread methods, like start or stop. Otherwise, it makes more sense to use Runnable.
 
Ramneek Handa
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Angela Ann:
The only reason I can think of to use Thread rather than Runnable would be if you need to override one of the other thread methods, like start or stop. Otherwise, it makes more sense to use Runnable.

Thanks Angela this seems to be a good reason. but still why Runnable makes more sense when Thread Class is not putting any overheads to it.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have derived from Thread in server type applications in order to be able to remotely query status data. You can get the ThreadGroup and then iterate through all active Threads - my derived Thread has a toString function that gives extra information about the status of that object.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!