• 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

Runnable r=new Runnable();

 
Ranch Hand
Posts: 96
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to page No.-682(Chapter Thread)
It's mentioned

I want to know how Runnable interface is instantiated.Even we can not instantiate abstract one.
Thanks
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bahubal:
Referring to page No.-682(Chapter Thread)
It's mentioned

I want to know how Runnable interface is instantiated.Even we can not instantiate abstract one.
Thanks



Hi,

From your code sample, you are right, it is not possible to instantiate Runnable in such way.

You could however create a anonymous Runnable. Could it be what they are doing ?(assuming you might have not post the entire code):


Regards,
Alex
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, considering as anonymous it is possible.There is only this much piece of code given ,so it's hard to understand.
Thanks Alex
 
Ranch Hand
Posts: 284
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak,
Here the Runnable mentioned i guess is not referring the Runnable interface, rather a Thread named as Runnable. And they meant to say that you wont be able to instantiate a new thread on calling run() method directly. It will be just a call to run method of the Thread like ordinary method calls. Below may clear it out a bit-:



Here we have a thread as named as Runnable, and calling start() on it creates a instance of it. As you can check out with the output-:

main
Inside class runnable
Thread-0



But when you comment out the start() and uncomment the run() , you will see just main thread running up as in below output-:

Inside class runnable
main
main



Thanks
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh that's sneaky!!
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pranav
I got the point,and it clarifies what is given in the book.But looking your code sample i have one more question to ask that how could you able to call start() with the instance of user defined class(here-Runnable),as it should be Thread class.
:roll:
Thanks
Deepak
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak
how could you able to call start() with the instance of user defined class(here-Runnable),as it should be Thread class.



Deepak,
I can do this cause my class is extending the Thread class so the start() gets inherited inside my Runnable class. This makes it eligible for running once start() is called
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pranav.
 
Ranch Hand
Posts: 107
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As has been pointed out here Runnable is an interface so you need to declare an anonymous inner class and implement the run() method.

Now, to run this as a thread you need to pass the Runnable to a thread. The Thread class has a number of different constructors, one of which takes a Runnable as a parameter:

 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ian Edwards:
As has been pointed out here Runnable is an interface so you need to declare an anonymous inner class and implement the run() method.

Now, to run this as a thread you need to pass the Runnable to a thread. The Thread class has a number of different constructors, one of which takes a Runnable as a parameter:



I think here, the trick part was that the book sample actually creates their own class named Runnable extending Thread..

So, it was possible to instantiate Runnable and start it.

Regards,
Alex
[ February 19, 2008: Message edited by: Alex Belisle Turcot ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic