zoukankan      html  css  js  c++  java
  • 多线程——实现Callable接口

    先说一下Runnable和Callable的区别:

    1、Callable规定的方法是call(),Runnable规定的方法是run().

    2、Callable的任务执行后可返回值,而Runnable的任务是不能返回值得

    3、call方法可以抛出异常,run方法不可以

    4、运行Callable任务可以拿到一个Future对象,表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。通过Future对象可以了解任务执行情况,可取消任务的执行,还可获取执行结果。

    例子:计算输入单词的长度,而且还要求计算出字符串数组中所有单词的长度之和。

    很明显,这样一改动,多线程的执行体就需要有一个返回值,用以计算所有单词的长度之和。而runnable中的run方法是不能有返回值的,所以,这里我们只能使用callable。具体代码如下:

     1 package test;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 import java.util.concurrent.Callable;
     6 import java.util.concurrent.ExecutionException;
     7 import java.util.concurrent.ExecutorService;
     8 import java.util.concurrent.Executors;
     9 import java.util.concurrent.Future;
    10 
    11 
    12 public class Test1{
    13     public static void main(String [] args ) {
    14         String [] words = {"first","second","world","thread"};
    15         
    16         //创建一个线程池
    17         ExecutorService pool = Executors.newCachedThreadPool(  );
    18         Set> set = new HashSet>();
    19         
    20         for (String word: words) {
    21             Callable callable = new testCallable(word);
    22             Future future = pool.submit(callable);
    23             set.add(future);
    24         }
    25         int sum = 0;
    26         for (Future future : set) {
    27             try {
    28                 sum += future.get();
    29             } catch (InterruptedException e) {
    30                 e.printStackTrace();
    31             } catch (ExecutionException e) {
    32                 e.printStackTrace();
    33             }
    34         }
    35         System.out.println("数组中所有单词的总长度为:" + sum);
    36     }
    37 
    38     
    39 }
    40 
    41 
    42 class testCallable implements Callable{
    43     private String word;
    44     
    45     public testCallable(String word){
    46         
    47         this.word = word;
    48     }
    49     
    50     @Override
    51     public Integer call() throws Exception {
    52         System.out.println(Thread.currentThread().getName() + ": 开始执行!" );
    53         try {
    54             //假设处理需要2秒
    55             Thread.currentThread().sleep(2000);
    56         } catch (InterruptedException e) {
    57             e.printStackTrace();
    58         }
    59         System.out.println(Thread.currentThread().getName() + ": 正在处理!" );
    60         System.out.println(Thread.currentThread().getName() + ": " + word + "长度为:" + word.length());
    61         return Integer.valueOf(word.length()); 
    62     }
    63 }

    执行结果如下:

    pool-1-thread-1: 开始执行!
    pool-1-thread-3: 开始执行!
    pool-1-thread-4: 开始执行!
    pool-1-thread-2: 开始执行!
    pool-1-thread-1: 正在处理!
    pool-1-thread-1: first长度为:5
    pool-1-thread-3: 正在处理!
    pool-1-thread-3: world长度为:5
    pool-1-thread-2: 正在处理!
    pool-1-thread-2: second长度为:6
    pool-1-thread-4: 正在处理!
    pool-1-thread-4: thread长度为:6
    数组中所有单词的总长度为:22

  • 相关阅读:
    IT北漂攻略—择业篇(程序员.net)
    Log4net和Log2Console 结合使用时出现的中文问题
    日志设计规范
    IT北漂攻略租房篇
    Log2Console日志监测工具使用方法
    一些好用的开源控件
    工作流设计简介
    代码设计规范
    正则表达式分割字符串但是不分割字符串中引号中间的数据
    解决Android 应用运行报Unable to resolve superclass of L错误
  • 原文地址:https://www.cnblogs.com/zq-boke/p/6991491.html
Copyright © 2011-2022 走看看