• 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

Run-time exception from type-casting

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

A class 'TestRunnable' implements Runnable.

IThe statements below execute in another class, but they are generating an exception:

Runnable runnable = Thread.getCurrentThread();
TestRunnable tRunnable = (TestRunnable)runnable;

The exception is: "Exception in thread "Thread-3" java.lang.ClassCastException: java.lang.Thread cannot be cast to TestRunnable"

I don't know why the exception is being generated, as both 'tRunnable' and 'runnable' implement Runnable...?

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would give you an example:

Can a Cat be a Dog? and vice versa? So that's what happens when you try to typecast among the siblings. So its not legal to typecast among the siblings. You can typecast up/down the hierarchy.
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not possible to type cast along different class hierarchies.
According to the scenario you have specified-
1> Thread implements Runnable (By Default)
2> tRunnable implements Runnable.
But, there exists no relationship in-between the Thread class and the tRunnable class...
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

The problem is I have this situation:

 
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
It's simple. If the object returned is truely an object that is that type, the type cast will succeed.

However, in your example, I am very sure that the Thread object returned is *not* your class type -- as your class type is not a Thread class type. Hence, it is not possible for you to use it to start a thread.

Henry
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mikpo Siru wrote:Thanks for your replies.

The problem is I have this situation:



You could as well call that method from the

of your TestRunnable class. So if its the currently executing thread, it will call the method. Usually your run() method will have the code for that thread of execution. So you can have all the related methods being invoked from the run() method.
 
Mikpo Siru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

TestRunnable implements Runnable and has a specific method that I want to call. I could make TestRunnable extend Thread. This will solve the type casting problem, but I don't want to restrict my options by extending a class at this stage.

 
Henry Wong
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

Mikpo Siru wrote:
TestRunnable implements Runnable and has a specific method that I want to call. I could make TestRunnable extend Thread. This will solve the type casting problem, but I don't want to restrict my options by extending a class at this stage.



In other words, you know for a fact that you TestRunnable class is *NOT* a Thread class. And you definitely don't want it to be a Thread class.

So... may I ask why do you think that the Thread object returned by the currentThread() method, could be a TestRunnable object?

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic