• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating Thread through Runnable Interface and through extension Class  RSS feed

 
Eugene Kris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between generating a Thread through using a class that implements Runnable interface and those who just directly generates it using extension class of Thread?

The one using Runnable interface has to be constructed through Thread class (Thread class constructor) eventually, so doesn't it make it the same with the one made through extension?

Just started learning Java so still confused with the concept here.

I'd appreciate any help. Thank you.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eugene Kris wrote:What's the difference between generating a Thread through using a class that implements Runnable interface and those who just directly generates it using extension class of Thread?

In terms of execution? Nothing.

The one using Runnable interface has to be constructed through Thread class (Thread class constructor) eventually, so doesn't it make it the same with the one made through extension?

Not quite true. A Thread can be constructed already linked to a Runnable, but the Runnable object itself is constructed just like any other.

Basically, a Runnable simply defines logic to be executed, and you can call its run() method directly if you want (not normally done before version 8; but possibly more useful now that it has been made a FunctionalInterface).

Also, because it's an interface, a class that implements Runnable can also extend another class, allowing you to create "hierarchies" of Runnables if you want, whereas a class that extends Thread can't.

Confusingly, Thread also implements Runnable, but this is simply so that you can create and run Threads in one swoop.

TBH, I wouldn't be at all surprised to see a default method (execute()?) added to Runnable in the near future, that simply executes:
  new Thread(this).start();

Just started learning Java so still confused with the concept here.

Don't worry, I've been doing Java for fifteen years and I still have to look up the APIs.

Winston
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you have really only just started learning Java®, I would say you do not need to know about Threads yet. It is easy enough to find Tutorials, however, which should explain the advantage of using Runnable over extending Thread. The new way of creating a Runnable in Java8 is to use a λ; the following can run the foo method in a separate Thread:-
Runnable r = () -> foo();
I think if you are a beginner, it is more profitable to learn about λs than about Threads. When you get to the stage of needing Threads, you will find most people use something like ExecutorService, which creates a certain number of Threads, and simply use the Threads it provides.
 
Eugene Kris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eugene Kris wrote:
What's the difference between generating a Thread through using a class that implements Runnable interface and those who just directly generates it using extension class of Thread?

In terms of execution? Nothing.
...

Basically, a Runnable simply defines logic to be executed, and you can call its run() method directly if you want (not normally done before version 8; but possibly more useful now that it has been made a FunctionalInterface).

Also, because it's an interface, a class that implements Runnable can also extend another class, allowing you to create "hierarchies" of Runnables if you want, whereas a class that extends Thread can't.


@Winston Thank you so much for your explanation. I'm not sure if i understood it all perfectly, but i think i'm beginning to get a better grasp on it.
 
Eugene Kris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Welcome to the Ranch

If you have really only just started learning Java®, I would say you do not need to know about Threads yet. It is easy enough to find Tutorials, however, which should explain the advantage of using Runnable over extending Thread. The new way of creating a Runnable in Java8 is to use a λ; the following can run he foo method in a separate Thread:-
Runnable rn = () -> foo();
I think if you are a beginner, it is more profitable to learn about λs than about Threads. When you get to the stage of needing Threads, you will find most people use something like ExecutorService, which creates a certain number of Threads, and simply use the Threads it provides.


@campbell You're probably right. Thanks for your input. I'll definitely going to check on it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7720
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:TBH, I wouldn't be at all surprised to see a default method (execute()?) added to Runnable in the near future, that simply executes:
  new Thread(this).start();

An execute() method would imply that there's a 1-to-1 relationship between a thread and a task. This is simply not true. A task can be performed by multiple threads, and one thread can perform multiple tasks. Tightly coupling the two is incredibly bad form.

It was a mistake of the language designers to make Thread implement Runnable in the first place, because a Thread doesn't represent a task.

Any newly written code shouldn't be touching the Thread class at all. Thread is a low level primitive that should be abstracted away by interfaces such as ExecutorService, or if you're using an application container, the framework's worker pool.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!