Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Overriding start() method of Threads

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any reason behind not overriding the start() method in a subclass of java.lang.Thread?
I found that instead of the thread class having to invoke the run() , you will have to do it explicitly.

Thanks!
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should almost never subclass Thread, and almost always implement Runnable. It's extremely rare that you'd need to override methods in the Thread class: ExtendingThreadVsImplementingRunnable
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I was just going through Marcus Green's exam # 1 and it had one question where it was overriding.
Also invoking start() without a thread object does not throw a compiler error, it just runs the current executing thread's run().
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it do it in the same thread or another thread ?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also invoking start() without a thread object does not throw a compiler error, it just runs the current executing thread's run()




it must be .start()
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reference : Marcus Greens exam no 1, question 19 ( it is not 1.6 but just wanted to clarify)


This is not overriding, but I wrote a simple class to check it out. But invoking start() in piggy() is confusing
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is confusing.

Here are some things to think about:

1. When you do start(), that actually means this.start(). What this means is that the current class must have a start() method (which all Thread instances have.) If you call start() in the code for a class which doesn't have a start() method you will get a compiler error.

2. You can only call java.lang.Thread.start() once on a given Thread instance, or you will get an IllegalThreadStateException What that means is that when you call start() within the code of a subclass of Thread, the call itself must take place in a thread other than the one that you are attempting to start. In other words, you are calling code inside a Thread class from a thread of execution that must be different than the thread of execution originated by the start() invocation. It also means that once the thread is alive, it can't call the start() code again or it will throw the exception (of course, you could always catch the exception and do something with it.)

I hope this didn't make things even more confusing!
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the pointers, this.start() would make it very clearer.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic