zoukankan      html  css  js  c++  java
  • Java Callable用法

    Callable 和 Runnable 的使用方法大同小异, 区别在于:
    1.Callable 使用 call() 方法, Runnable 使用 run() 方法
    2.call() 可以返回值, 而 run()方法不能返回。
    3.call() 可以抛出受检查的异常,比如ClassNotFoundException, 而run()不能抛出受检查的异常。
    Callable示例如下:

    Java代码:

    class TaskWithResult implements Callable<String> {
       private int id;

       public TaskWithResult(int id) {
           this.id = id;
       }

       @Override
       public String call() throws Exception {
          return "result of TaskWithResult " + id;
         }
      }

    public class CallableTest {
       public static void main(String[] args) throws InterruptedException,ExecutionException {
         ExecutorService exec = Executors.newCachedThreadPool();
         ArrayList<Future<String>> results = new ArrayList<Future<String>>(); //Future 相当于是用来存放Executor执行的结果的一种容器
        for (int i = 0; i < 10; i++) {
            results.add(exec.submit(new TaskWithResult(i)));
        }
        for (Future<String> fs : results) {
            if (fs.isDone()) {
              System.out.println(fs.get());
          } else {
              System.out.println("Future result is not yet complete");
            }
        }
        exec.shutdown();
       }
    }

    执行结果:

    result of TaskWithResult 0
    result of TaskWithResult 1
    result of TaskWithResult 2
    result of TaskWithResult 3
    result of TaskWithResult 4
    result of TaskWithResult 5
    result of TaskWithResult 6
    result of TaskWithResult 7
    result of TaskWithResult 8
    result of TaskWithResult 9

    Runnable示例:

    public class LiftOff implements Runnable {

       protected int countDown = 10;
       private static int taskCount = 0;
       private final int id = taskCount++;

       public LiftOff() {}

       public LiftOff(int countDown) {
           this.countDown = countDown;
       }

       public String status() {
          return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff! ") + ")";
           }

       @Override
       public void run() {
           while (countDown-- > 0) {
             System.out.print(status());
             Thread.yield();
          }
          System.out.println();
       }

       public static void main(String[] args) {
         ExecutorService exec = Executors.newFixedThreadPool(1);
           for (int i = 0; i < 5; i++) {
             exec.execute(new LiftOff());
          }
          exec.shutdown();
       }
    }

    执行结果:

    #0(9)#0(8)#0(7)#0(6)#0(5)#0(4)#0(3)#0(2)#0(1)#0(LiftOff! )
    #1(9)#1(8)#1(7)#1(6)#1(5)#1(4)#1(3)#1(2)#1(1)#1(LiftOff! )
    #2(9)#2(8)#2(7)#2(6)#2(5)#2(4)#2(3)#2(2)#2(1)#2(LiftOff! )
    #3(9)#3(8)#3(7)#3(6)#3(5)#3(4)#3(3)#3(2)#3(1)#3(LiftOff! )
    #4(9)#4(8)#4(7)#4(6)#4(5)#4(4)#4(3)#4(2)#4(1)#4(LiftOff! )

  • 相关阅读:
    struts2自定义拦截器之过滤不良言论---http500可能的问题所在
    bzoj4205[FJ2015集训] 卡牌配对
    bzoj1562[NOI2009] 变换序列
    bzoj1433[ZJOI2009] 假期的宿舍
    bzoj2150 部落战争
    从bzoj2463到bzoj1443和bzoj2437 博弈+二分图匹配
    bzoj4554[Tjoi2016&Heoi2016] 游戏
    bzoj1059[ZJOI2007] 矩阵游戏
    bzoj1143[CTSC2008] 祭祀river
    bzoj3175[Tjoi2013] 攻击装置
  • 原文地址:https://www.cnblogs.com/zhongle/p/2675023.html
Copyright © 2011-2022 走看看