threadpool抛出java.util.concurrent.futuretask不能施放到java.lang.com.
我有一个非常简单的程序:
public static void main(String[] args) throws Exception {
class MyThread2 implements Runnable{
@Override
public void run() {
while (true) {
try {
if (Thread.interrupted()) {
System.out.println("Terminated!");
throw new InterruptedException();
}
} catch (InterruptedException e) {
return;
}
}
}
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
1,
TimeUnit.SECONDS,
new PriorityBlockingQueue<>(),
Executors.defaultThreadFactory());
executor.submit(new MyThread2());
executor.submit(new MyThread2());
executor.submit(new MyThread2());
executor.shutdownNow();
}
在使用Maven运行它时,它可以打印:
[WARNING]
java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable
at java.util.concurrent.PriorityBlockingQueue.siftUpComparable (PriorityBlockingQueue.java:357)
at java.util.concurrent.PriorityBlockingQueue.offer (PriorityBlockingQueue.java:489)
at java.util.concurrent.ThreadPoolExecutor.execute (ThreadPoolExecutor.java:1371)
at java.util.concurrent.AbstractExecutorService.submit (AbstractExecutorService.java:112)
at MyThreadFactory.main (MyThreadFactory.java:33)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:748)
挂起。我在程序中没有任何PriorityBlockingQueue
的“可比较”实现,此例外来自哪里?
I've got a very simple program:
public static void main(String[] args) throws Exception {
class MyThread2 implements Runnable{
@Override
public void run() {
while (true) {
try {
if (Thread.interrupted()) {
System.out.println("Terminated!");
throw new InterruptedException();
}
} catch (InterruptedException e) {
return;
}
}
}
}
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2,
2,
1,
TimeUnit.SECONDS,
new PriorityBlockingQueue<>(),
Executors.defaultThreadFactory());
executor.submit(new MyThread2());
executor.submit(new MyThread2());
executor.submit(new MyThread2());
executor.shutdownNow();
}
On running it using maven, it prints:
[WARNING]
java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable
at java.util.concurrent.PriorityBlockingQueue.siftUpComparable (PriorityBlockingQueue.java:357)
at java.util.concurrent.PriorityBlockingQueue.offer (PriorityBlockingQueue.java:489)
at java.util.concurrent.ThreadPoolExecutor.execute (ThreadPoolExecutor.java:1371)
at java.util.concurrent.AbstractExecutorService.submit (AbstractExecutorService.java:112)
at MyThreadFactory.main (MyThreadFactory.java:33)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
at java.lang.Thread.run (Thread.java:748)
And hangs. I don't have any "Comparable" implementation for PriorityBlockingQueue
in my program, where does this exception came from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PriorityBlockingQueue
期望可比元素,您的mythread2
类无法提供。因此,您的程序中确实具有可比性,但这是内部的。例如,如果您无法提供可比的元素,则将您的
executor
更改为linkedblockingqueue
。或显然,找到一种“解释”如何比较提交任务的方法(通过实现可比
)。现在我考虑一下,如果API可以检测到这一点,那将是非常酷的。因此,如果您提供
PriorityBlockingQueue
并调用crist(new MyThread2())
,在编译时间提高(说mythread2
不是可比
)。不幸的是,在此API中并非如此,我也不认为这是可能的。PriorityBlockingQueue
expects comparable elements, which yourMyThread2
class fails to provide. So you do haveComparable
in your program, but it's an internal thing.Change your
executor
to useLinkedBlockingQueue
, if you can't provide comparable elements, for example. Or obviously, find a way to "explain" how to compare your tasks that you submit (by implementingComparable
).Now that I think about it, it would have been very cool if the API could detect that. So if you provide a
PriorityBlockingQueue
and callsubmit(new MyThread2())
, at compile time, this would have detected and a compile time error raised (saying thatMyThread2
is not aComparable
). Unfortunately this is not the case in this API, nor I think it's possible at all.抛出异常,因为优先级阻止队列不知道,如何比较其元素。它不允许不可分配的对象。您有两个选项:
签名priorityblocking queue javadoc: https://docs.oracle.com/javase/7/docs/api/java/java/util/concurrent/concurrent/priorityblockingqueue.html
Exception is thrown, because priority blocking queue doesn't know, how to compare its elements. It does not permit non-comparable objects. You have two options:
Checkout PriorityBlocking queue javadoc: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/PriorityBlockingQueue.html
我得到了同样的例外,但是在某些情况下,我确实具有可比较的任务。我发现,如果称为小于或等于核心线程的数量,则提交作品。
Windows上的JDK版本是:
这是我拥有的代码,并在底部输出。
输出:
I got the same exception but for a little bit different case where I do have the task implement Comparable. What I found is that submit works if called less than or equal to the number of core threads.
JDK version on Windows is:
Here is the code I have and output at the bottom.
Output: