aspose file tools*
The moose likes Java in General and the fly likes ExecutorService is forcing me to cast to (Callable<T>) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ExecutorService is forcing me to cast to (Callable<T>)" Watch "ExecutorService is forcing me to cast to (Callable<T>)" New topic
Author

ExecutorService is forcing me to cast to (Callable<T>)

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Purpose:
Make it easy to multi-thread a group of callable objects, using a variable number of threads. Have the thread that ran the Thread_Handler.setupThreads() block until all the threads finish. Then return the finished objects back the the thread that ran Thread_Handler.setupThreads().Please note the comment. This error is requiring me to cast the executed object... I don't see why I would have to. Any suggestions?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Rooks Forgenal wrote:Please note the comment. This error is requiring me to cast the executed object... I don't see why I would have to. Any suggestions?

T, at that point, can be of any type. ExecutorService#submit() takes a Future. Not all Objects are Futures, therefore the compiler can not assume that object to run is a Callable. Try making it like:
Then, the compiler should know that every possible T is a callable, and you should not have to cast it. I think - I haven't tried.


Steve
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
OK, I tried it and yes, that works great. I should have known it need to know the object was callable. However, How do I instantiate this object now?This does not work....
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

MyObj has to implement Callable<MyObj>
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Works like a charm! Thank you so much for your help. I have updated the code to match what was said in this thread. Thanks again!
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Do I have to change this:To:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ExecutorService is forcing me to cast to (Callable<T>)