线程池的操作

线程池,有三种方式:

1.阻塞等待resultList获取到所有异步执行的结果才会执行

         //  创建线程池
        ExecutorService executeService = Executors.newCachedThreadPool();
        //  设置线程池集合
        List<GetContentTask> taskList = new ArrayList<GetContentTask>();
        long startTime = System.currentTimeMillis();
        //  添加集和
         taskList.add(new GetContentTask("http://1.3.127.131:8888/eoffice-restful/resources/sys/oadata", "usercode=10012")); 
        try {
            System.out.println("主线程发起异步任务请求");
            List<Future<String>> resultList = executeService.invokeAll(taskList);
            // 这里会阻塞等待resultList获取到所有异步执行的结果才会执行
            for (Future<String> future : resultList) {
                System.out.println(future.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

2.主线程空闲,可以去执行其他操作

        ExecutorService executorService = Executors.newCachedThreadPool();
        CompletionService<String> completionService = new    ExecutorCompletionService(executorService);
        // 十个
        long startTime = System.currentTimeMillis();
        int count = 2;
            GetContentTask getContentTask = new GetContentTask("动tia","http://112.118.10.89:8080/eoffice-restful/resources/sys/oadata", "usercode=10012");
            completionService.submit(getContentTask);
        getContentTask = new GetContentTask("df组","http://122.128.22.82:8280/eoffice-restful/resources/sys/oadata", "usercode=10012");
        completionService.submit(getContentTask);
        getContentTask  = new GetContentTask("ga组","http://api.map.baidu.com/telematics/v3/weather", "location=嘉兴&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ");
        completionService.submit(getContentTask);
        System.out.println("提交完任务,主线程空闲了, 可以去做一些事情。");
        try {
            //  结果
          for (int i = 0;i < 3;i ++) {
                Future<String> result = completionService.take();
                System.out.println(result.get());
            }
            long endTime = System.currentTimeMillis();
            System.out.println("耗时 : " + (endTime - startTime) / 1000);
        }  catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

3.等待一段时间,不论线程执行完毕,都结束

try {
            ExecutorService executorService = Executors.newCachedThreadPool();
            List<Callable<String>> taskList = new ArrayList<Callable<String>>();
            taskList.add(new GetContentTask("动tia","http://112.118.10.89:8080/eoffice-restful/resources/sys/oadata", "usercode=10012"));
            
            taskList.add(new GetContentTask("df组","http://122.128.22.82:8280/eoffice-restful/resources/sys/oadata", "usercode=10012"));
            // 等待五秒 
            List<Future<String>> resultList = executorService.invokeAll(taskList, 10, TimeUnit.SECONDS);
            for (Future<String> future : resultList) {
                if(future.isCancelled()){
                    System.err.println(resultList.indexOf(future));
                    GetContentTask tesk= (GetContentTask) taskList.get(resultList.indexOf(future));
                    System.err.println(tesk.getName());
                }else{
                    System.out.println(future.get());
                }
            }
            System.err.println("线程执行完毕"); 
        } catch (Exception e) {
            e.printStackTrace();
        }

线程model类

  
package cn.thread;

import com.sun.jndi.toolkit.url.UrlUtil;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author EDZ
 * @create 2019/1/15
 * @since 1.0.0
 */
public class GetContentTask implements Callable<String> {
    private String url;
    private String param;
    private String name;

    public GetContentTask(String name, String url, String param) {
        this.name = name;
        this.url = url;
        this.param = param;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String call() throws Exception {
          return name+"线程调用,地址为"+url;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值