• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

MultiThreaded Application

 
Ranch Hand
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't created a thread myself since I was in school and we're learned about the concept.
Now I need to create a couple of threads myself in an application cause some actions need to be performed asynchronously, so I was reading up on the concept.
If I got it right you need to extend the Threadclass or implement the runnable interface and overwrite the run method, which is being executed once the thread is being started.
That way I need to create a seperate class or interfaceimplementation for each action that might be needed.
Isn't there a simple way, like simply returning the thread an overwrite the run method on the spot, instead of hardcoding it?
 
Rancher
Posts: 4504
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isn't there a simple way


By simple do you mean fewer lines of code?  For example a lambda expression: https://www.codejava.net/java-core/the-java-language/java-8-lambda-runnable-example
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pieter Vandevelde wrote:If I got it right you need to extend the Threadclass or implement the runnable interface and overwrite the run method, which is being executed once the thread is being started.



If you're reading something which suggests extending Thread, then you're reading the wrong thing. There is no need to extend Thread.

But you also mentioned that you aren't in school any longer. In that case you don't need to create a new Thread to run an action asynchonously. Use an ExecutorService instead. Here's a tutorial about how to do that. You'll notice that, as Norm suggested, it uses a lambda-expression to implement Runnable.
 
You showed up just in time for the waffles! And this tiny ad:
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
reply
    Bookmark Topic Watch Topic
  • New Topic