I want to instantiate a new java ThreadPoolExecutor with this piece of code:
public class ImageList {
private LinkedBlockingQueue<Image> list;
private final ThreadPoolExecutor executor;
public ImageList() {
executor = new ThreadPoolExecutor(2, 4, 100, TimeUnit.SECONDS, list);
}
}
Where Image has the following header:
public class Image implements Runnable, Serializable
However, Java complains that a constructor for the type BlockingQueue<Runnable> was not found. What am I doing wrong?
The constructor expects a
BlockingQueue<Runnable>. You pass it aBlockingQueue<Image>.A
BlockingQueue<Image>is not aBlockingQueue<Runnable>. Indeed, you may store any kind of Runnable in aBlockingQueue<Runnable>, but you may only storeImageinstances in aBlockingQueue<Image>.If it were, you could do the following:
and boom! your
BlockingQueue<Image>would contain something other than anImage.