This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Can I start a Thread subclass as a lambda?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can write:
 
But when I write:
 
I get an error:
 
I've tried creating various constructors for MyThread, and I've looked at the Thread source code trying to understand, but I haven't been able to figure out why it works for Thread but not for MyThread.  I
 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are not inherited. Your MyThread class only defines one no-argument constructor. The compiler can't find any other constructor that it can match to your attempt to instantiate MyThread with a lambda expression. You need to declare a MyThread constructor that takes a Runnable as its argument if you want to do that.

 
Junilu Lacar
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!
 
Saloon Keeper
Posts: 12154
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that threads really should not be extended. Heck, they shouldn't even be constructed directly.

In professional code, use an ExecutorService and submit Runnable or Callable.
 
Van Wanless
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not extend Thread?
 
Junilu Lacar
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Van Wanless wrote:Why not extend Thread?



Well, this: Why not extend Thread?
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Van Wanless wrote:Why not extend Thread?


Here's a question back: why extend Thread? What functionality would you like to add that isn't already present?

Extending Thread just to override the run method is unnecessary as you can give a Runnable to the constructor. A Runnable instance is reusable (can be used in multiple threads), a Thread instance can only be started once. And since Java 8 it's even easier to use a lambda for a Runnable, whereas if you extend Thread you must be as verbose as always:
 
Van Wanless
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, as I started this topic:

As Junilu pointed out in his very helpful answer, this is equivalent to declaring an anonymous Runnable.  Even if I had declared an explicit Runnable, I'd still have to spin up a thread to run it.  
And yes, I do know about Futures and Callables and ThreadPoolExecutors.  But that wasn't the question I asked.  I have looked at the web trying to get an answer to the Thread vs Runnable question, and despite special pleading for Runnables as "preserving the option to inherit from other than class Thread", and "Runnables can be reused" there are lots of times when these just aren't an issue.  As far as I can tell, it boils down to personal preference.
 
Stephan van Hulst
Saloon Keeper
Posts: 12154
258
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes, it's personal preference. Take a look at the following application:

Why did I extend Rectangle? You might chalk it up to my eccentric preference to make everything fit in boxes. Similarly, there's never a good technical reason to extend Thread; it only serves to confuse programmers that don't share the same preference.

No application IS a rectangle. No runnable task IS a method of execution of said task.
 
Van Wanless
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is actually the best and clearest explanation I have ever heard.
 
Yes, my master! Here is the tiny ad you asked for:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic