*
The moose likes Java in General and the fly likes how to cast superclass into subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to cast superclass into subclass" Watch "how to cast superclass into subclass" New topic
Author

how to cast superclass into subclass

Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
Hi all,

I have this problem and I can't seem to find on the Internet for a solution.
I have this class called Example. Here is the code snippet.



I am using a ScheduledThreadPoolExecutor class. Under that class, there is this method called getQueue.
However the getQueue return a BlockingQueue of Runnable class. Now is the question. Is there a way to cast the Runnable class into Example class?
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
My apology for the mistake. Runnable is an interface but still I would like to ask if there is a way to return the Example class from the BlockingQueue.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
I will try use instanceof and casting to see if it works.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Why do you need to cast?

ScheduledThreadPoolExecutor will run your Runnable instances regardless of what the exact class of the implementation is; this is the beauty of polymorphism. And once the executor runs your Example instance, the Example.run() method gets executed, at this point you already know it's the Example instance and don't need any cast at all.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
@Martin Vajsar I want to do cast is because I want to keep track of the ID. Notice that in the Example I have implemented ID. Because I could not find a way to access a thread in the ScheduledThreadPoolExecutor, thus I decided to put an ID in the Runnable so that I can check whether I have any existing ID running.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Your Example instances could register and unregister themselves from the run method; that way you could maintain a list of active instances, and you would not have to cast anything (unregistering would have to be made from a finally block). However, I'm not sure whether this is optimal solution in your situation. Maybe if you posted the code where you intend to cast, we could continue from there?
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22
Your Example instances could register and unregister themselves from the run method; that way you could maintain a list of active instances, and you would not have to cast anything (unregistering would have to be made from a finally block). However, I'm not sure whether this is optimal solution in your situation. Maybe if you posted the code where you intend to cast, we could continue from there?


Hi Martin, what do you mean by register and unregister?

This is the way I do casting.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

In your case I believe the casting is appropriate. You might want to check whether you should synchronize on the list while you iterate over it or not (I'm not sure about it, but I believe you should synchronize).

What I had on my mind was this: create a Set to keep IDs of running Example instances (most easily a static Set in the Example class itself, and pay attention to proper synchronization). Then, at the beginning of the Example.run() method, add the instance ID into this set, and in a finally block inside the same method, remove the ID from the set. That way this set will always contain IDs of active Example instances. I don't know whether this is better than your current approach, though.
Tim Lim
Greenhorn

Joined: Oct 14, 2010
Posts: 22

In your case I believe the casting is appropriate. You might want to check whether you should synchronize on the list while you iterate over it or not (I'm not sure about it, but I believe you should synchronize).

What I had on my mind was this: create a Set to keep IDs of running Example instances (most easily a static Set in the Example class itself, and pay attention to proper synchronization). Then, at the beginning of the Example.run() method, add the instance ID into this set, and in a finally block inside the same method, remove the ID from the set. That way this set will always contain IDs of active Example instances. I don't know whether this is better than your current approach, though.


Hi Martin, thanks for your input, I will consider it. Anyway I have opened a thread pertaining to the ScheduledThreadPoolExecutor class in this thread since this discussion is about casting.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to cast superclass into subclass
 
Similar Threads
Thread tricky questions.
Mock Blues 1
java threads
Synchornized inside run
Shutting down a thread instantly - should this work?