zoukankan      html  css  js  c++  java
  • ExecutorService invokeAll 实例(转)

     10个班级,每个班级20名学生,在指定的时间内查询每个班级学生的集合。 

    Java代码  收藏代码
      1. package cn.com.ld.study.thread;  
      2.   
      3. import java.util.ArrayList;  
      4. import java.util.Collection;  
      5. import java.util.HashMap;  
      6. import java.util.List;  
      7. import java.util.Map;  
      8. import java.util.concurrent.Callable;  
      9. import java.util.concurrent.ExecutionException;  
      10. import java.util.concurrent.ExecutorService;  
      11. import java.util.concurrent.Executors;  
      12. import java.util.concurrent.Future;  
      13. import java.util.concurrent.TimeUnit;  
      14.   
      15. public class FutureTest {  
      16.     //缓存操作数据集  
      17.     private static final Map<Integer, List<Student>> sutdenMap = new HashMap<Integer, List<Student>>();  
      18.     //初始化操作数据  
      19.     static {  
      20.         List<Student> stuList = null;  
      21.         Student stu;  
      22.         for (int i = 0; i < 10; i++) {  
      23.             stuList = new ArrayList<Student>();  
      24.             for (int j = 0; j < 2; j++) {  
      25.                 stu = new Student(j, "zld_" + i + "." + j, i);  
      26.                 stuList.add(stu);  
      27.             }  
      28.             sutdenMap.put(i, stuList);  
      29.         }  
      30.     }  
      31.   
      32.     public static class Student {  
      33.         private int id;  
      34.         private String name;  
      35.         private int classID;  
      36.   
      37.         public Student(int id, String name, int classID) {  
      38.             this.id = id;  
      39.             this.name = name;  
      40.             this.classID = classID;  
      41.         }  
      42.   
      43.         public String toString() {  
      44.             return Student.class.getName() + "(id:" + this.id + ",name:"  
      45.                     + this.name + ")";  
      46.         }  
      47.   
      48.     }  
      49.       
      50.     /**    
      51.      * @filename: SearchTask    
      52.      * @description: 查询任务   
      53.      * @author lida   
      54.      * @date 2013-4-1 下午3:02:29       
      55.      */  
      56.     public static class SearchTask implements Callable<List<Student>> {  
      57.   
      58.         public final int classID;  
      59.         public long sleepTime;  
      60.   
      61.         /**    
      62.          * <p>Title: </p>    
      63.          * <p>Description: </p>    
      64.          * @param classID  班级编号 
      65.          * @param sleepTime  模拟操作所用的时间数(毫秒) 
      66.          */  
      67.         SearchTask(int classID, long sleepTime) {  
      68.             this.classID = classID;  
      69.             this.sleepTime = sleepTime;  
      70.         }  
      71.   
      72.         @Override  
      73.         public List<Student> call() throws Exception {  
      74.             //模拟操作所用的时间数(毫秒)  
      75.             Thread.sleep(sleepTime);  
      76.             List<Student> stuList = sutdenMap.get(classID);  
      77.             return stuList;  
      78.         }  
      79.   
      80.     }  
      81.   
      82.     public static void main(String[] args) {  
      83.         FutureTest ft = new FutureTest();  
      84.         ExecutorService exec = Executors.newCachedThreadPool();  
      85.         List<SearchTask> searchTasks = new ArrayList<SearchTask>();  
      86.         SearchTask st;  
      87.         for (int i = 0; i < 10; i++) {  
      88.             st = new SearchTask(i, 2001);//指定2001 毫米为最大执行时间  
      89.             searchTasks.add(st);  
      90.         }  
      91.   
      92.         try {  
      93.             //要求认为在2000毫秒内返回结果,否则取消执行。  
      94.             List<Future<List<Student>>> futures = exec.invokeAll(searchTasks,  
      95.                     2000, TimeUnit.MILLISECONDS);//invokeAll 第一个参数是任务列表;第二个参数是过期时间;第三个是过期时间单位  
      96.             for (Future<List<Student>> future : futures) {  
      97.                 List<Student> students = future.get();  
      98.                 for (Student student : students) {  
      99.                     System.out.println(student.toString());  
      100.                 }  
      101.             }  
      102.             exec.shutdown();  
      103.         } catch (InterruptedException e) {  
      104.             e.printStackTrace();  
      105.             Thread.interrupted();  
      106.         } catch (ExecutionException e) {  
      107.             e.printStackTrace();  
      108.         }  
      109.     }  
      110. }  
      111. http://zld406504302.iteye.com/blog/1840091
  • 相关阅读:
    ST_Geometry效率的测试与分析
    ArcEngine中加载ArcGIS Server地图服务
    正则表达式入门教程&&经典Javascript正则表达式(share)
    实现文件上传,以及表单提交成功的回调函数
    Jquery+asp.net实现Ajax方式文件下载实例代码
    Jquery 中 ajaxSubmit使用讲解
    其它课程中的python---4、Matplotlib最最最最简单使用
    其它课程中的python---3、numpy总结(非常全)
    其它课程中的python---2、NumPy模块
    其它课程中的python---1、python基础
  • 原文地址:https://www.cnblogs.com/softidea/p/5029501.html
Copyright © 2011-2022 走看看