3

I was recently reading about creation of Threads in java by implementing Runnable or Extending thread and last Implementing Callable. Runnable Versus Callable thread at stackoverflow describes the difference quoting both are designed for classes whose instances are potentially executed by another thread. What does it mean? Does it creates new Thread? If yes, why we need to pass a class that implements Runnable to Thread constructor?

Also, i saw the method of creating threads by implementing Runnable or Extending thread. In the first method, (in the tutorials what i found), we need to call Thread class which requires the Runnable instance to start the thread. But, i could not found the similar thing for Callable as there is no Thread constructor which accepts Callable. Executor framework or Future Task is used for the purpose of running those threads. Then why we say both ways are same (except Callable retruns something and can throw Exception).

Last, is writing

Thread t = new Thread();
Thread t1 = new Thread(new RunnableInstance());

Do these create new Threads in system? Is there any other way of using Runnable to create new threads without passing it as a constructor to Thread class?

It should not be a duplicate question.

Community
  • 1
  • 1
Vivek Vardhan
  • 1,118
  • 3
  • 21
  • 44
  • 1
    Executors are modern way to create thread in java where developer has more handle on thread and it has thread pool. `ExecutorService#submit` you submit runnable instance also. – Subhrajyoti Majumder Aug 19 '14 at 12:20
  • @SubhrajyotiMajumder Thanks for reply. Can you answer all other ponits also. Your comment only partially answers it. – Vivek Vardhan Aug 19 '14 at 12:25

3 Answers3

3

What does it mean? Does it creates new Thread?

Both Callable and Runnable are just interfaces, they don't create any threads by themself. Instead they provide API and abstractions for developers. When you want to execute some code in separate thread you typically implement Runnable and then you can decide how to execute this. It is not bound to any thread yet. You have many options actually:

  • Execute it in the new Thread
  • Execute it with some ExecutorService
  • Or just call it directly

If yes, why we need to pass a class that implements Runnable to Thread constructor?

No. Since Runnable does not create thread behind (well, it simply can't since it's just an interface!), we need to execute this Runnable explicitly.

Do these create new Threads in system?

Yes.

Is there any other way of using Runnable to create new threads without passing it as a constructor to Thread class?

Yes. I mentioned already ExecutorService. You can profit from thread pool or completion service, take a look at the API and examples.

nogard
  • 9,432
  • 6
  • 33
  • 53
  • SO, it means, we can execute the classes which implements `Runnable` or `Callable` using any of the method, not specific to what i wrote in question. Is it true? Except for classes that implements `Callable` can not be run in the new Thread. Am i right? – Vivek Vardhan Aug 19 '14 at 12:41
  • I am getting confused again. If `Runnable` is just an interface which is not bound to any thread, then what is its use? We can have any interface say `IMyInterface` in which I will have `run` method, then any class implementing this interface will need to bound it to new thread either by executing it in new Thread or execute it with Executor service. Running directly will run it under main thread. Is it correct? – Vivek Vardhan Apr 23 '15 at 07:02
  • Or may be not, because Thread does not have any constructor that accepts `IMyInterface` as the argument. But, it accepts `Runnable`, that's why we need to implement `Runnable` – Vivek Vardhan Apr 23 '15 at 07:08
1

Callable

It will return the result of the execute.

Runnable

It wont return. But it will run separately like callable.

Extends Thread

Its also a runnable. But if you extend thread , you cant extend any class since java wont support multiple inheritance.

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
1

Callable and Runnable provides interfaces for other classes to execute them in threads. They contain no functionality of their own. The most common way to do this is via an ExecutorService. Have a look at the classes available in java.until.concurrent. There are many options there. Extending Thread is not really called for unless you really intend to add new low-level threading functionality.

jiggy
  • 3,828
  • 1
  • 25
  • 40