zoukankan      html  css  js  c++  java
  • Callable、Future和FutureTask使用说明

    普通的创建线程,一种是直接继承Thread,另外一种就是实现Runnable接口。但是这两种都无法在执行完任务之后获取执行结果,Callable、Future就提供了这样的便利。
     
    Future的方法说明:
     
    boolean cancel(boolean mayInterruptIfRunning);
         cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。
    1. 参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。
    2. 如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;
    3. 如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;
    4. 如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
    boolean isCancelled();
         isCancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true。
    boolean isDone();
         isDone方法表示任务是否已经完成,若任务完成,则返回true;
    V get() throws InterruptedException, ExecutionException;
         get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回;
    V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
         get(long timeout, TimeUnit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null。
     
    而FutureTask即可以作为Runnable又可以作为Future,这样就既可以用ExecutorService的execute执行任务,也可以用ExecutorService的submit提交任务。
     
    例子1:
    Callable、Future实现子线程执行任务,并返回结果,主线程等待子线程结果在进行其他逻辑。(多个子线程并行执行任务,主线程做合并处理参见CompletionService用法
     
     1 import java.util.concurrent.Callable;
     2 import java.util.concurrent.ExecutionException;
     3 import java.util.concurrent.ExecutorService;
     4 import java.util.concurrent.Executors;
     5 import java.util.concurrent.Future;
     6  
     7 public class CallableAndFuture1 {
     8  
     9         public static void main(String[] args) {
    10               ExecutorService service = Executors.newSingleThreadExecutor();
    11  
    12               Future<Integer> future = service.submit( new Callable<Integer>() {
    13  
    14                       @Override
    15                       public Integer call() throws Exception {
    16                            System. out.println("子线程" + Thread.currentThread().getName() + "在进行计算");
    17                            Thread. sleep(3000);
    18  
    19                             int sum = 0;
    20                             for (int i = 0; i < 100; i++) {
    21                                   sum += i;
    22                            }
    23                             return sum;
    24                      }
    25               });
    26  
    27               System. out.println("主线程" + Thread.currentThread ().getName() + "在执行任务" );
    28  
    29                try {
    30                      System. out.println("子线程运行结果" + future.get());
    31               } catch (InterruptedException e) {
    32                      e.printStackTrace();
    33               } catch (ExecutionException e) {
    34                      e.printStackTrace();
    35               }
    36  
    37               System. out.println("所有任务执行完毕" );
    38  
    39        }
    40 }
  • 相关阅读:
    如何使用Log4j
    HDU 1114
    老鼠与毒药问题
    HDU 1065
    HDU 1301(MST)
    HDU 1078
    HDU 2159
    删除字符问题(贪心)
    正整数分解为几个连续自然数之和
    OpenJudge
  • 原文地址:https://www.cnblogs.com/tstd/p/5031329.html
Copyright © 2011-2022 走看看