首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中的内部FutureTask

Java中的内部FutureTask
EN

Stack Overflow用户
提问于 2012-12-12 21:40:42
回答 2查看 992关注 0票数 2

我尝试实现一个内部方法,以便在新线程中执行以下代码

代码语言:javascript
复制
MyPojo result = null;
final MyPojo result2 = result;

FutureTask<MyPojo> runnableTask = new FutureTask<MyPojo>(
    new Runnable() {  

        BindJSON<MyPojo> binding;

        // Make the URL at which the product list is found
        String sourceURLString = 
            "http://www.....ca/files/{CAT_ID}.json";                

        @Override
        public void run() {  
            sourceURLString = sourceURLString.replace("{CAT_ID}", catId);
            binding = new BindJSON<MyPojo>();  
            result2 = binding.downloadData(sourceURLString, MyPojo.class);  
        }  
    }, 
    result2);

runnableTask.run();

所以,现在我得到一个错误:最后一个局部变量result2不能赋值,因为它是在封闭类型中定义的。我看了一下这个答案:Cannot refer to a non-final variable i inside an inner class defined in a different method,但它对我不起作用。我应该怎么做才能让它工作呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-12 21:53:09

您可能希望使用Callable,而不是Runnable

代码语言:javascript
复制
// the variable holding the result of a computation
String result = null;

FutureTask<String> runnableTask = new FutureTask<String>(
        new Callable<String>() {
            public String call() throws Exception {
                // (asynchronous) computation ...
                return "42";
            }
        });

System.out.println("result=" + result); // result=null

// this will invoke call, but it will all happen in the *same thread*
runnableTask.run();

// to have a parallel thread execute in the 'background'
// you can use java.util.concurrent.Executors
// Note: an ExecutorService should be .shutdown() properly
// Executors.newSingleThreadExecutor().submit(runnableTask);

// waits for the result to be available
result = runnableTask.get();

// you can also add timeouts:
// result = runnableTask.get(100, TimeUnit.MILLISECONDS);

System.out.println("result=" + result); // result=42

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html

  • Executors
票数 4
EN

Stack Overflow用户

发布于 2012-12-12 21:52:51

您对FutureTask的使用不是很有用,因为您在同一个线程中执行它。您可以使用executor并提交一个callable来实现您想要的功能:

代码语言:javascript
复制
    ExecutorService executor = Executors.newFixedThreadPool(1);

    Callable<MyPojo> task = new Callable<MyPojo> () {
        BindJSON<MyPojo> binding;
        // Make the URL at which the product list is found
        String sourceURLString = "http://www.....ca/files/{CAT_ID}.json";

        @Override
        public MyPojo call() {
            sourceURLString = sourceURLString.replace("{CAT_ID}", catId);
            binding = new BindJSON<MyPojo>();
            return binding.downloadData(sourceURLString, MyPojo.class);
        }
    };

    Future<MyPojo> future = executor.submit(task);
    MyPojo result = future.get();

注意:对future.get();的调用将被阻塞,直到任务完成。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13841096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档