I have this problem and I can’t seem to find on the Internet for a solution. I have posted a similar link here, http://www.coderanch.com/t/581517/java/java/cast-superclass-subclass#2645691
class Example implements Runnable {
String ID;
public Example (String ID) {
this.ID = ID;
}
public void run() {
// Code to do something.
}
}
I am using a ScheduledThreadPoolExecutor class. Under that class, there is this method called getQueue. However the getQueue return a BlockingQueue of Runnable interface.
Is there a way to get back an Example class from a Blocking queue of Runnable interface?
Are you inserting new tasks into the queue using the schedule method of ScheduledThreadPoolExecutor?
Are these tasks instaces of the Example class?
If so, then when obtaining a new “Runnable” you can cast it to Example by:
Depending on the flow of your program it may be a good idea to verify that the types match before casting (for example using instanceof).
Hope it helps.