• 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

Accessing a Runnable Interface method from its thread?

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

how to call a method in a class implemnting Runnable interface from its Thread?

consider the following scenario


Is there any way i can access the method mthdToAccess()using the object t?


Regards,
Ajay.
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
t.mthdToAccess();
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't do it using t. Since the method is part of RunImplementor, you could use a RunImplementor reference instead:

Or another possibility is, if you really need to do something like this, you could extend Thread rather than implementing Runnable. Then mthdToAccess() would be part of your new Thread subclass. Usually I wouldn't recommend this unless mthdToAccess() is somehow directly related to the normal responsibilities of a Thread. But it's hard to say without knowing more about what you're trying to do here.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Thanks for your reply. But the real scenario is

I need to access mthdToAcess() from a different class which only holds the reference to object t and i don't want to loose the state of RunnableImpl by creating a new object for it.

How to acheive this?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ajay]: I need to access mthdToAcess() from a different class which only holds the reference to object t and i don't want to loose the state of RunnableImpl by creating a new object for it.

Hm, I don't really understand how creating an object loses state. However:

[Ajay]: How to acheive this?

"Or another possibility is, if you really need to do something like this, you could extend Thread rather than implementing Runnable. Then mthdToAccess() would be part of your new Thread subclass.""
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
His question is actually how to get back wrapped runnable instance from Thread instance?

Once he get back runnable instance, then he can call mthdToAccess() method from some other method of a different class.

I just saw API documentation, there is no such method in Thread class which can give you wrapped runnable instance.

Why don't you pass both as an argument in that method.

Regards

Naseem
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should keep the reference to the object you need to reference later and throw away the reference to the thread, not the other way around.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The optimal soln. in this scenario is to extend the class Thread, and get your own version of Thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic