Android : Java BlockingQueue

on Tuesday, September 16, 2014


I'm using a ThreadPoolExecutor in some code, and I supply it with a BlockingQueue<Runnable>. I get a compiler error saying can't resolve constructor. So I tried casting queue explicitly to BlockingQueue<Runnable>. However, I'm getting a compiler error by the ThreadPoolExecutor's constructor:



Inconvertible types, cannot cast PriorityBlockingQueue<MyRunnable> to BlockingQueue<Runnable>


This is happening even though PriorityBlockingQueue implements BlockingQueue and MyRunnable implements Runnable. I'm using Java 7 on Android.


Code:



public class Test {
PriorityBlockingQueue<MyRunnable> queue = new PriorityBlockingQueue<>(...);

//compiler error without cast
ThreadPoolExecutor executor = new ThreadPoolExecutor(..., queue, ...);
//compiler error with cast
ThreadPoolExecutor executor = new ThreadPoolExecutor(..., (BlockingQueue<Runnable> queue, ...);

public static class MyRunnable implements Runnable {...}


Temporary Solution:


In order to get it to compile, I've changed the cast to (BlockingQueue), but I get a unchecked case warning (which I suppressed). I know that I can make queue a BlockingQueue<Runnable> but then I have to cast in a bunch of places throughout my code which I'd rather not do.


Is there something that I'm missing, or is the unchecked warning my best shot.


0 comments:

Post a Comment