转自:http://blog.csdn.net/hanlipenghanlipeng/article/details/52108446
package com.concurrency;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
*
* @author Sailing
*
*/
public class ThreadPool {
private class HelloRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name+"----it's runnable");
}
}
Runnable runnable = new HelloRunnable();
/**
* 创建大小不固定的线程池
*
* 如果线程池大小超过了处理任务所需要的线程
* 线程池就会回收空闲的线程池,当处理任务增加时,线程池可以增加线程来处理任务
* 线程池不会对线程的大小进行限制
* 线程池的大小依赖于操作系统
**/
ExecutorService es = Executors.newCachedThreadPool();
public void getThreadPool(){
for(int i=0;i<12;i++){
es.execute(runnable);
// System.out.println("currentName----"+Thread.currentThread().getName());
try { //如果没有这一步,会出现多个不同的线程,也会有线程重用;如果使得主线程睡一下,那么只用一个线程就够了
Thread.sleep(0, 1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("isShutdown() : "+es.isShutdown()+" isTerminated "+es.isTerminated());
}
es.shutdown();
}
/**
*Console 如果主线程不睡,会出现多个不同的线程,也会有线程重用
* pool-1-thread-1----it's runnable
pool-1-thread-2----it's runnable
pool-1-thread-3----it's runnable
pool-1-thread-4----it's runnable
pool-1-thread-3----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-4----it's runnable
pool-1-thread-2----it's runnable
pool-1-thread-5----it's runnable
pool-1-thread-6----it's runnable
pool-1-thread-7----it's runnable
pool-1-thread-8----it's runnable
*
* Console 主线程睡一下
* pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
pool-1-thread-1----it's runnable
*/
/**创建具一个可重用的,有固定数量的线程池
* 每次提交一个任务就提交一个线程,直到线程达到线城池大小,就不会创建新线程了
* 线程池的大小达到最大后达到稳定不变,如果一个线程异常终止,则会创建新的线程
*/
ExecutorService es1 = Executors.newFixedThreadPool(20);
public void getThreadPool1(){
for(int i=0;i<12;i++){
es1.execute(runnable);
}
es1.shutdown();
}
/**
*Console 如果创建的线程池够用,不会重用。如果不够,则会重用
* pool-2-thread-3----it's runnable
pool-2-thread-1----it's runnable
pool-2-thread-2----it's runnable
pool-2-thread-4----it's runnable
pool-2-thread-5----it's runnable
pool-2-thread-6----it's runnable
pool-2-thread-8----it's runnable
pool-2-thread-9----it's runnable
pool-2-thread-10----it's runnable
pool-2-thread-12----it's runnable
pool-2-thread-11----it's runnable
pool-2-thread-7----it's runnable
*/
/**创建只有一个线程的线程池
* 按照提交顺序执行
* 跟上个数量为1的是一样
*/
ExecutorService es2 = Executors.newSingleThreadExecutor();
public void getThreadPool2(){
for(int i=0;i<12;i++){
es2.execute(runnable);
}
es2.shutdown();
}
/**
* Console
* pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
pool-3-thread-1----it's runnable
*/
/**
* 创建一个线程池,大小可以设置,此线程支持定时以及周期性的执行任务 定时任务
*/
ScheduledExecutorService ses = Executors.newScheduledThreadPool(5);
public void getScheduledTreadPool(){
ses.scheduleAtFixedRate(runnable, 0, 100, TimeUnit.MICROSECONDS);//参数1:执行的目标对象 参数2:延时多长时间开始执行 参数3:开始后,多长时间执行一次 参数4:时间单位
}
/**
* Console
* pool-4-thread-5----it's runnable
pool-4-thread-5----it's runnable
pool-4-thread-5----it's runnable
pool-4-thread-5----it's runnable
pool-4-thread-2----it's runnable
pool-4-thread-2----it's runnable
pool-4-thread-2----it's runnable
pool-4-thread-4----it's runnable
pool-4-thread-4----it's runnable
pool-4-thread-3----it's runnable
pool-4-thread-3----it's runnable
pool-4-thread-1----it's runnable
pool-4-thread-1----it's runnable
pool-4-thread-1----it's runnable
*/
public static void main(String[] args) {
//new ThreadPool().getThreadPool();
//new ThreadPool().getThreadPool1();
//new ThreadPool().getThreadPool2();
new ThreadPool().getScheduledTreadPool();
}
}