侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索
MC

java.util.concurrent.FutureTask

Dettan
2021-07-10 / 0 评论 / 0 点赞 / 116 阅读 / 829 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
本质就是继承自 Runable 和 Future, 就是一个可以获取运行结果的线程任务.
Future就是运行结果
Future<String> future
       = executor.submit(new Callable<String>() {
         public String call() {
             return searcher.search(target);
         }});

public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);

boolean isDone();

V get() throws InterruptedException, ExecutionException;

V get(long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException;

}




调用下面的方法运行.
@Nullable
public static <V> V runTask(FutureTask<V> task, Logger logger)
{
    try
    {
        task.run();
        return task.get();
    }
    catch (ExecutionException executionexception)
    {
        logger.fatal("Error executing task", (Throwable)executionexception);
    }
    catch (InterruptedException interruptedexception)
    {
        logger.fatal("Error executing task", (Throwable)interruptedexception);
    }
return (V)null;

}

0

评论区