• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 5008
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: 28193
95
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic