简体   繁体   English

为什么java.util.concurrent.FutureTask不可序列化

[英]Why is java.util.concurrent.FutureTask not serializable

I am currently using Apache Wicket. 我目前正在使用Apache Wicket。 I have some REST calls which take a few seconds each. 我有一些REST调用,每个调用几秒钟。 Wicket allows ajax calls synchronously only, so I was trying to use Future and Callable. Wicket只允许同步调用ajax,因此我尝试使用Future和Callable。

This is part of my class: 这是我班级的一部分:

public abstract class GetPrices extends AbstractAjaxTimerBehavior {
        private static final long serialVersionUID = 1L;    
        private List<Future<List<Result>>> list;

        public GetPrices(Duration updateInterval, List<Callable> priceCalls) {
           super(updateInterval);
           list = new ArrayList<Future<List<Result>>>();

           ExecutorService executor = Executors.newFixedThreadPool(priceCalls.size());
           for(Callable callable : priceCalls) {
               list.add(executor.submit(callable));
           }
           executor.shutdown();
        }

        @Override
        protected void onTimer(AjaxRequestTarget target) {
            while(list.hasNext()) {
                Future<List<Result>> future = listIterator.next();
                if(future.isDone()) {
                    List<Result> data = future.get();
                    //Process data
                }
            }
        }
    //Error handling etc
}

The List<Callable> priceCalls contains the method calls to the appropriate price calls List<Callable> priceCalls包含对相应价格调用的方法调用

I receive The object type is not Serializable! 我收到的对象类型不是Serializable! against java.util.concurrent.FutureTask list field 针对java.util.concurrent.FutureTask列表字段

I am assuming I should design this differently. 我假设我应该以不同的方式设计。 Could any provide any thoughts on how this should be done? 可以提出任何关于如何做到这一点的想法吗?

I am using Spring if that helps 如果有帮助我正在使用Spring

FutureTask不可序列化,因为它依赖于其他资源,如Executor ,可能是Thread实例, Queue和OS资源,无法轻松序列化。

Another option would be create your own implementation of Future, which also implements Serializable. 另一种选择是创建自己的Future实现,它也实现了Serializable。

In the page below, the author shows that. 在下面的页面中,作者显示了这一点。 In this case, he used JMS behind the Future implementation. 在这种情况下,他在Future实现后面使用了JMS。

http://www.javacodegeeks.com/2013/02/implementing-custom-future.html http://www.javacodegeeks.com/2013/02/implementing-custom-future.html

I ended up using Spring's Async and AjaxLazyLoadPanel 我最终使用了Spring的Async和AjaxLazyLoadPanel

See more info here: http://apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188 点击此处查看更多信息: http//apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 5:java.util.concurrent.FutureTask - cancel()和done()的语义 - Java 5: java.util.concurrent.FutureTask - Semantics of cancel() and done() 这是一个使用java.util.concurrent.FutureTask的好方法吗? - Is it a good way to use java.util.concurrent.FutureTask? ThreadPool 抛出 java.util.concurrent.FutureTask 无法转换为 java.lang.Comparable - ThreadPool throws java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable 为什么TimeUnit是java.util.concurrent的成员? - Why is TimeUnit a member of java.util.concurrent? 为什么并发HashMap可序列化 - Why Concurrent HashMap is Serializable 为什么java.util.Optional不可序列化,如何序列化object有这些字段 - Why java.util.Optional is not Serializable, how to serialize the object with such fields 为什么java.util.List不能实现Serializable? - Why doesn't java.util.List implement Serializable? java.util.concurrent中 - java.util.concurrent 为什么java.util.concurrent.RunnableFuture有run()方法? - Why does java.util.concurrent.RunnableFuture have a run() method? 为什么使用数组而不是链表的java.util.concurrent.PriorityBlockingQueue - Why is java.util.concurrent.PriorityBlockingQueue using an array not a linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM