//线程池简介
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test implements Runnable{
//固定大小的线程池
public static void main(String[] args) {
//固定的线程池:池子里面的线程数量是固定的
// ExecutorService servicePool = Executors.newFixedThreadPool(2);
//缓存线程池:池子里面的线程数量是不固定的
// ExecutorService servicePool = Executors.newCachedThreadPool();
//池子里面只有一个线程,如果该线程死掉了,则会重新创建一个线程来代替死掉的线程
//如何实现线程死掉后重新启动?:使用只有一个线程的线程池
ExecutorService servicePool = Executors.newSingleThreadExecutor();
Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
Test t4 = new Test();
servicePool.execute(t1);
servicePool.execute(t2);
servicePool.execute(t3);
servicePool.execute(t4);
}
@Override
public void run() {
System.out.println("正在执行任务:"+Thread.currentThread().getName());
}
}
// 利用Callable和Future接口 来获取线程执行结果
<span style="font-size:14px;">import java.util.concurrent.Callable;
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;
public class Test2 {
//newSingleThreadExecutor future:Future结合使用 ,用来获取线程执行后的返回结果
public static void main(String[] args) {
//Callable :要用ExecutorService 的submit()方法提交,返回future对象.
//future:Future获取的返回类型和Callbable提交的类型一致,这个是由泛型来实现的。
//结合使用 ,用来获取线程执行后的返回结果
ExecutorService executorService = Executors.newSingleThreadExecutor();
//Callable在这里可以类比runable 来理解一下
Future<String> resultFuture = executorService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
//*****规定3秒后结果才出来
Thread.sleep(3000);
System.out.println("等待2秒后提交(执行任务,和execute类似)...");
return "threadpool start task";
}
});
try {
//get(等的时间,时间单位)为了获取计算结果至多等给定的时间.注意是至多,
//*****这里如果是等1,2秒的情况,则肯定超时(timeoutexception)如果是3秒,和上面结果出来时的时间段一致,经测试
//有可能发生timeoutexception(这个程序运行不确定,有可能超时,有可能不超时),但如果是4秒,则大于上面3秒出结果。则不会
//发生timeoutexception
String returnString = resultFuture.get(4, TimeUnit.SECONDS);
//get()一直等带获取结果。没有就一致等。
//String returnString = resultFuture.get();
System.out.println("规定至多等4秒获取结果:"+returnString);
//结束线程池
executorService.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}</span><span style="font-size:18px;font-weight: bold;">
</span>
// 获取一组线程的执行结果import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Test3 {
////newFixedThreadPool CompletionService Callable future:Future结合使用 ,用来获取一组线程执行后的返回结果
public static void main(String[] args) throws Exception {
Random r = new Random();
//CompletionService 用于来执行一组任务,其take方法返回
//已经完成的一个任务的执行结果(Future对象)
//固定大小的池子:10
ExecutorService executorService = Executors.newFixedThreadPool(10);
//用来执行一个线程池里的任务,这里理解为包装一下线程池。
CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
//用for循环执行一组任务
for(int i=0;i<10;i++){
final int tempIndex = i;
completionService.submit(new Callable<String>() {
@Override
public String call() throws Exception {
//任务同时异步运行,如果暂停时间一致的话,则出来结果会很快速,这里设置间隔大一些。
Thread.sleep(r.nextInt(10000));
return "任务号:"+tempIndex;
}
});
}
for(int i=0;i<10;i++){
//take方法返回先执行完的任务的结果,如果有任务还没执行完,则等待.(阻塞方法)
Future<String> result = completionService.take();
String firstResult = result.get();
System.out.println("依次执行完的任务:"+firstResult);
}
executorService.shutdownNow();
}
}