线程池,有三种方式:
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;
}
}