码迷,mamicode.com
首页 > 编程语言 > 详细

Java_InvokeAll_又返回值_多个线程同时执行,取消超时线程

时间:2015-08-30 19:22:15      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

package com.demo.test4;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @author QQ: 1236897 如果超过限制时间则取消超时线程
 *
 */
public class InvokeDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ExecutorService exec = Executors.newCachedThreadPool();
        List<SearchTask> searchTasks = new ArrayList<SearchTask>();
        SearchTask st = null;
        for (int i = 0; i < 10; i++) {
            st = new SearchTask(i, 200);// 指定2001 毫米为最大执行时间
            if (i == 5)
                st = new SearchTask(i, 2001);// 指定2001 毫米为最大执行时间
            searchTasks.add(st);
        }

        try {
            // 要求认为在2000毫秒内返回结果,否则取消执行。
            List<Future<List<Person>>> futures = exec.invokeAll(searchTasks,
                    2000, TimeUnit.MILLISECONDS);// invokeAll
                                                    // 第一个参数是任务列表;第二个参数是过期时间;第三个是过期时间单位
            int count = 0;
            for (Future<List<Person>> future : futures) {
                try {
                    List<Person> students = future.get();
                    for (Person student : students) {
                        System.out.println(student.toString());
                    }
                } catch (CancellationException e) {
                    System.out.println("cancel");
                    future.cancel(true);
                }
                System.out.println("-----------------------" + count
                        + "--------------------");
                count++;
            }
            exec.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.interrupted();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}

/**
 * @filename: SearchTask
 * @description: 查询任务
 * @author lida
 * @date 2013-4-1 下午3:02:29
 */
class SearchTask implements Callable<List<Person>> {

    public final int classID;
    public long sleepTime;

    /**
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     * 
     * @param classID
     *            班级编号
     * @param sleepTime
     *            模拟操作所用的时间数(毫秒)
     */
    SearchTask(int classID, long sleepTime) {
        this.classID = classID;
        this.sleepTime = sleepTime;
    }

    @Override
    public List<Person> call() throws Exception {
        // 模拟操作所用的时间数(毫秒)
        Thread.sleep(sleepTime);
        List<Person> stuList = new ArrayList<Person>();
        Person p = new Person(1, "name", 2);
        Person p2 = new Person(2, "name", 3);
        stuList.add(p);
        stuList.add(p2);
        return stuList;
    }

}

class Person {
    private int id;
    private String name;
    private int classID;

    public Person(int id, String name, int classID) {
        this.id = id;
        this.name = name;
        this.classID = classID;
    }

    public String toString() {
        return Person.class.getName() + "(id:" + this.id + ",name:" + this.name
                + ")";
    }

}

 

Java_InvokeAll_又返回值_多个线程同时执行,取消超时线程

标签:

原文地址:http://www.cnblogs.com/MarchThree/p/4771225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!