1.使用线程池 ThreadPoolExecutor
构造方法:ThreadPoolExecutor(int corePoolSize, int maxinumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
corePoolSize:线程池维护线程的最少数量
maxinumPoolSize:线程池维护线程的最大数量
keepAliveTime:线程池维护线程所允许的空闲时间
unit:线程池维护线程所允许的空闲时间的单位
workQueue:线程池所使用的缓冲队列
handler:线程池对拒绝任务的处理策略
2.需要注意以下几点:
(1)一个任务通过execute(Runnable)方法被添加到线程池,任务就是一个Runnable类型的对象,
任务队的执行方法就是Runnable类型对象的run()方法。
(2)当一个任务通过execute(Runnable)方法欲添加到线程池时;
①如果此时线程池中的线程的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程
来处理被添加的任务。
②如果此时线程池中的线程的数量等于corePoolSize,但是缓冲队列workQueue未满,那么任务被放入缓冲队列。
③如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量小于
maxinumPoolSize,创建新的线程来处理被添加的任务。
④如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量等于
maxinumPoolSize,那么通过handler所制定的策略来处理此任务。也就是:处理任务的优先级为:核心线程
corePoolSize,任务队列workQueue,最大线程maxinumPoolSize,如果三者都满了,使用handler
实现Callable<V>
构造方法:ThreadPoolExecutor(int corePoolSize, int maxinumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
corePoolSize:线程池维护线程的最少数量
maxinumPoolSize:线程池维护线程的最大数量
keepAliveTime:线程池维护线程所允许的空闲时间
unit:线程池维护线程所允许的空闲时间的单位
workQueue:线程池所使用的缓冲队列
handler:线程池对拒绝任务的处理策略
2.需要注意以下几点:
(1)一个任务通过execute(Runnable)方法被添加到线程池,任务就是一个Runnable类型的对象,
任务队的执行方法就是Runnable类型对象的run()方法。
(2)当一个任务通过execute(Runnable)方法欲添加到线程池时;
①如果此时线程池中的线程的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程
来处理被添加的任务。
②如果此时线程池中的线程的数量等于corePoolSize,但是缓冲队列workQueue未满,那么任务被放入缓冲队列。
③如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量小于
maxinumPoolSize,创建新的线程来处理被添加的任务。
④如果此时线程池中的线程的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的线程数量等于
maxinumPoolSize,那么通过handler所制定的策略来处理此任务。也就是:处理任务的优先级为:核心线程
corePoolSize,任务队列workQueue,最大线程maxinumPoolSize,如果三者都满了,使用handler
处理被拒绝的任务。
3.使用实例:
创建类 实现Runnable
public class EmailSender implements Runnable{
//Shift + Alt + S + V 重写从父类继承过来的方法
@Override
public void run() {
// System.out.println("发送数据");
try {
Thread.currentThread();
Thread.sleep(Math.round(Math.random()*0.5*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
主方法:<span style="white-space:pre"> </span>ThreadPoolExecutor execute = new ThreadPoolExecutor(50,
100, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
for (int i = 0; i < 10000; i++) {
EmailSender sender = new EmailSender();
execute.execute(sender);
}
//释放资源
execute.shutdown();
//监控
while(!execute.isTerminated()){
execute.awaitTermination(1, TimeUnit.SECONDS);
int process = Math.round((execute.getCompletedTaskCount() * 100)/execute.getTaskCount());
System.out.println(process + "%," + execute.getCompletedTaskCount() + "发送完毕。");
}
实现Callable<V>
public class EmailSenderC implements Callable<Boolean>{
//Shift + Alt + S + V 重写从父类继承过来的方法
@Override
public Boolean call() throws Exception{
// System.out.println("发送数据");
try {
Thread.currentThread();
Thread.sleep(Math.round(Math.random()*0.5*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
//模拟成功率
if(Math.random() > 0.7){
return false;
}
return true;
}
}
<span style="white-space:pre"> </span>ThreadPoolExecutor execute = new ThreadPoolExecutor(50,
100, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000));
List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
for (int i = 0; i < 10000; i++) {
EmailSenderC sender = new EmailSenderC();
Future<Boolean> submit = execute.submit(sender);
futures.add(submit);
}
//释放资源
execute.shutdown();
//监控
while(!execute.isTerminated()){
execute.awaitTermination(1, TimeUnit.SECONDS);
int process = Math.round((execute.getCompletedTaskCount() * 100)/execute.getTaskCount());
System.out.println(process + "%," + execute.getCompletedTaskCount() + "发送完毕。");
}
//统计
int succeesCount = 0;
int failureCount = 0;
for (Future<Boolean> future : futures) {
if (future.get()) {
succeesCount++;
}else{
failureCount++;
}
}
System.out.println("成功:"+ succeesCount + ",失败:" + failureCount);