0

我有这样的代码..

1) 使用反射调用 updatedb 方法...

for (String uniqueSym : activeSymbolsSet) {
    futureTaskUtil.submiteTask(new Helper(),
    Helper.class.getDeclaredMethod("updateDb", 
           new Class<?>[] { String.class }), new Object[] { uniqueSym }, 60);
}

- 未来任务工具:

2)我的问题是这个 updatedb 是作为运行时任务执行的......

public Object submiteTask(final Object obj, final Method method, final Object[] params, int timeoutSeconds) throws Exception {
    if (null != obj && method != null) {
        Callable<Object> task = new Callable<Object>() {
        public Object call() {
            try {
                method.setAccessible(true);
                Object resultObj = method.invoke(obj, params);
                return resultObj;
            } catch (Exception e) {
                logger.fatal("Exception occured while invoking future task.", e);
            }
            return null;
        }
    };
    Future<Object> future = executor.submit(task);
    try {
        Object result = null;
        if (timeoutSeconds < 0) {
            result = future.get(timoutsec, TimeUnit.SECONDS);
        } else {
            result = future.get(timeoutSeconds, TimeUnit.SECONDS);
        }
        logger.info("Result of method execution is  :: " + result);
        return result;
    } catch (TimeoutException e) {
    } catch (Exception e) {
        logger.fatal("Exception occured while executing future tas : " + obj, e);
    } finally {
        future.cancel(true); // may or may not desire this
    }
}
return null;
}

有人可以解释为什么这是作为一项单独的任务执行并调用该方法吗?

4

2 回答 2

0

如果方法永远不会返回怎么办?然后应用程序将在此时挂起。使用此包装器,该方法将在 60 秒后取消。

于 2012-09-10T11:39:15.107 回答
0

Future<Object> future = executor.submit(task);这是你的罪魁祸首您可以在此处阅读有关 Executer 框架的信息

于 2012-09-10T11:31:21.907 回答