• 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

Threads

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i've seen no argument thread constructor ie., Thread();

but what's use of that? doesn't the thread useless without a job(Runnable)?

is there anyother reason behind that no argument Thread.?

help me get over this ?

thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ssiva raman wrote:
i've seen no argument thread constructor ie., Thread();

but what's use of that? doesn't the thread useless without a job(Runnable)?

is there anyother reason behind that no argument Thread.?




Although not recommended, you can also subclass from the Thread class.

Henry
 
ssiva raman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but Mr.henry what is the point of subclassing the Thread?
instead we can create new Thread and pass it with different runnable object?
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ssiva raman wrote:is there anyother reason behind that no argument Thread.?


It simplifies your task of creating an anonymous subclass of Thread on the run like this:


If there wasn't any 0-arg constructor in Thread, then the anonymous subclass would have to invoke the parameterized constructor explicitly, which you can't do.
 
ssiva raman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i've studied threads using above sort of code i.e, implementing runnable in different class and passing its reference to Thread constructor.and there is no use of Thread otherwise.
your code is bit of strange to me.perhaps i'm not having a deep knowledge in threads.so can you please elaborate your code,if you dont bother, in the sense how do implement run() method in Thread unless you implement runnable in that class.
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ssiva raman wrote:in the sense how do implement run() method in Thread unless you implement runnable in that class.


Thread class itself implements Runnable and has overridden the run() method which looks like this:

Source Here. So, when you create a class implementing Runnable, and pass it's instance to the Thread constructor, the run() method of Thread will in turn call the run() method in your class. But if you just want to create a single thread, then it might not make much sense to create a separate class for this. This is where comes the use of anonymous inner class. The syntax like below:

... creates an anonymous subclass of Thread class, and overrides the run() method. Now, invoking the start() on that instance will call the run() method you have overridden there. So, it is giving you the similar outcome as you get by creating a different class implementing Runnable.
 
ssiva raman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does Thread class implicitly implements Runnable?
 
R. Jain
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ssiva raman wrote:does Thread class implicitly implements Runnable?


Yes. It does. You always have the javadoc to see these stuffs.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic