3

谁能说出为什么在以下代码段中使用 aFutureTask不起作用?
我的理解是我们可以使用FutureTask,对吗?
给出一个完整的描述:
我在我的代码中偶然发现了一些微妙的错误。
简而言之,我创建了一个使用 aSingleThreadPoolExecutor来运行Callables 作为参数传递的类。
我使用它来运行后台任务并在 Eclipse 应用程序中显示进度对话框。
代码是:

public class TaskRunner <T>  {  

    final static ExecutorService threadPool = Executors.newSingleThreadExecutor();  
    private T taskResult;  

    public T runTask(Callable<T> task, Shell currentShell) throws CustomException{          
        if(currentShell == null){  
            currentShell = new Shell();  
        }  
        final Callable<T> taskToRun = task;       

        ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(currentShell);  
        try {  
            progressDialog.run(false, true, new IRunnableWithProgress() {  

                @SuppressWarnings("unchecked")  
                @Override  
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {  
                    monitor.beginTask("Saving your data!", 100);  

                    FutureTask<T> task = createTask(taskToRun);  
                    Future<?> result = threadPool.submit(task);  
                    while(!result.isDone()){  
                        monitor.worked(1);  
                    }                     
                    try {  
                        taskResult = (T) result.get();  
                        if(taskResult == null){  
                            System.out.println("Result here in return is NULL!");  
                        }  

                    } catch (ExecutionException e1) {  
                        logger.error(e1);  
                    }   
                    catch(Exception e){  
                        logger.error(e);  
                    }               
                }  
            });  
        } catch (InvocationTargetException e) {  
            logger.error(e);  
        } catch (InterruptedException e) {  
            logger.error(e);  
        }       
        return taskResult;  
    }  

    private static <T> FutureTask<T> createTask(Callable<T> theTask){  
        return new FutureTask<T>(theTask);   
    }  
}  

和代码任务:

public class MyTask implements Callable<ComplexObject> {    

    private Util helper;  
    private String filePath;  

    public LoadKeystoreTask(Util helper, String filePath) {  
        this.helper = helper;     
        this.filePath = filePath;  
    }  

    @Override  
    public Container call() throws Exception {        
        ComplexObject result = helper.loadData(filePath);  
        if(result == null){  
            System.out.println("ComplexObject IS NULL!");  
        }  
        else{  
            System.out.println("ComplexObject IS NOT NULL!");  
        }   
        return result;  
    }  
}   

问题:
虽然helper.loadData正确地返回了结果(通过调试和打印语句验证)这一行:

taskResult = (T) result.get(); 

总是null。_
即为了简化它被打印:

复杂对象不为空!
这里的结果是 NULL!

经验证,这是由提交FutureTask.
如果我提交一个Callable

即更改为:

Future<?> result = threadPool.submit(taskToRun);

有用!为什么 wrapping withFutureTask会导致这个问题?

4

2 回答 2

2

FutureTask这里是不必要的:

private static <T> FutureTask<T> createTask(Callable<T> theTask){  
    return new FutureTask<T>(theTask);   
}

FutureTask<T> task = createTask(taskToRun);  
Future<?> result = threadPool.submit(task); 

直接submit() Calleble<T>

Future<?> result = threadPool.submit(taskToRun); 

我认为不FutureTask应该在用户代码中使用:

这个类提供了一个基本的实现Future

将实施留给Future图书馆。

于 2012-10-27T20:55:21.590 回答
0

这很简单。FutureTask所以你打电话Runnable的确切方法是。现在让我们看一下javadoc:ExecutorServiceFuture<?> submit(Runnable task)

提交 Runnable 任务以执行并返回代表该任务的 Future。Future 的 get 方法将在成功完成后返回null ......

这是非常可以理解的。Runnable仅包含void run()方法。你希望它返回什么结果?

如果您提交FutureTask只是Executor使用它来获取结果:

FutureTask<Integer> task = ...
threadPool.submit(task);
result = task.get();
于 2012-11-30T00:10:40.973 回答