Why is java.util.concurrent.FutureTask not serializable

南笙酒味 提交于 2019-12-13 19:52:32

问题


I am currently using Apache Wicket. I have some REST calls which take a few seconds each. Wicket allows ajax calls synchronously only, so I was trying to use Future and 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

I receive The object type is not Serializable! against java.util.concurrent.FutureTask list field

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


回答1:


FutureTask isn't serializable because it depends on other resources like an Executor, probably a Thread instance, a Queue and OS resources which can't be easily serialized.




回答2:


Another option would be create your own implementation of Future, which also implements Serializable.

In the page below, the author shows that. In this case, he used JMS behind the Future implementation.

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




回答3:


I ended up using Spring's Async and AjaxLazyLoadPanel

See more info here: http://apache-wicket.1842946.n4.nabble.com/AjaxLazyLoadPanel-loading-asynchronously-td4664035.html#a4665188



来源:https://stackoverflow.com/questions/22375764/why-is-java-util-concurrent-futuretask-not-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!