3 Replies - 917 Views - Last Post: 06 May 2010 - 07:51 PM Rate Topic: -----

#1 Guest_Sujatha*


Reputation:

Which is reliable? Runnable In or Thread Class for creating threads?

Posted 05 May 2010 - 10:34 PM

We can create thread in two ways.

Extending Thread class
Implementing Runnable Interface.

Which is reliable method?

Why run() cannot be called directly and can be called only thro' start()?
Is This A Good Question/Topic? 0

Replies To: Which is reliable? Runnable In or Thread Class for creating threads?

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12211
  • View blog
  • Posts: 45,290
  • Joined: 27-December 08

Re: Which is reliable? Runnable In or Thread Class for creating threads?

Posted 05 May 2010 - 10:47 PM

Since Thread accepts a Runnable object as a param in the Constructor, you can use either. Some things to consider though. If you are already extending another class or having a current class you want to execute in parallel to whatever is running, you may want to implement Runnable since Java does not support multiple inheritance. However, if want a Thread to perform an action without being part of a heirarchy, better to subclass Thread and override run(). Really, it's more of a design choice and highly dependent on your problem and program organization.

As for your question about the invocation of run(), the JVM invokes run() and handles the parralelization once you invoke start(). The start() method also handles some error checking before starting the Thread.

For more information on Threads, you may want to check out the API:
http://java.sun.com/...ad.html#start()
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,009
  • Joined: 11-December 07

Re: Which is reliable? Runnable In or Thread Class for creating threads?

Posted 06 May 2010 - 12:35 AM

You can call the run method. The problem is that it won't do what you want. Every java program has threads. When the main method is called at the start of a program, it is running in a thread. If you call run() from that the code in the run method will execute. However, it will run in the main thread instead of it's own thread. If you call six run() methods from six runnables, they will run but they'll run one after the other inthere same thread.

However, if you call start() the virtual machine creates a new thread each time and those six run() methods will run concurrently and not have to wait for each other.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Which is reliable? Runnable In or Thread Class for creating threads?

Posted 06 May 2010 - 07:51 PM

Basically if your Class does not extends any other class you should

class Abc extends Thread

if it extends another class you should implement runnable

class Abc extends JPanel implements Runnable
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1