3 Replies - 422 Views - Last Post: 17 April 2011 - 08:52 AM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 381
  • Joined: 14-December 08

Newbie Thread question

Posted 16 April 2011 - 09:11 PM

I know how to use thread in Java...But there is one thing that baffles me, which is the thread class itself.

My question is really simple. Since we have the thread class and of course, we could create a thread object, why do we still need to extend the Thread class...

I know implementing the runnable interface makes some sense to me as we have to use the object of the runnable interface in the thread constructor or implements its run method but come how we still need to extend the thread class?

Can't we just do something like this already:
Thread threadObject = new Thread();

Is This A Good Question/Topic? 0
  • +

Replies To: Newbie Thread question

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,796
  • Joined: 27-December 08

Re: Newbie Thread question

Posted 16 April 2011 - 09:18 PM

Sure. Except that the new Thread you are creating will simply die. The whole point of extending Threads are to customize their actions, usually by overriding the run() method. If you really know what you are doing, you can overload and override existing Thread methods. I wouldn't recommend it, but it's possible.

In addition, you may need to customize the Thread based on certain additional inputs. For example, I wrote a Multithreaded Mergesort snippet, which utilizes instance fields and additional methods. You could not get this level of customization and encapsulation as cleanly with an interface.

Hope this helps some. :)
Was This Post Helpful? 1
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 381
  • Joined: 14-December 08

Re: Newbie Thread question

Posted 16 April 2011 - 11:56 PM

View Postmacosxnerd101, on 16 April 2011 - 10:18 PM, said:

Sure. Except that the new Thread you are creating will simply die. The whole point of extending Threads are to customize their actions, usually by overriding the run() method. If you really know what you are doing, you can overload and override existing Thread methods. I wouldn't recommend it, but it's possible.


Could you be a bit more specific about the thread will die? Besides, it is interesting to know that the method could be overloaded...When overloading, do we just do the same thing as what we have been doing with the regular overloading or are there any restrictions that we gotta follow?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,796
  • Joined: 27-December 08

Re: Newbie Thread question

Posted 17 April 2011 - 08:52 AM

Your Thread will be created, nothing will happen, it will terminate, then it will be garbage collected. In short, it will die.

There are no further restrictions with method overloading or overriding.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1