Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a Runnable Interface method from its thread?  RSS feed

 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • 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.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t.mthdToAccess();
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.""
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • 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
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Yilmaz Mete
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The optimal soln. in this scenario is to extend the class Thread, and get your own version of Thread.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!