线程池
线程池类ThreadPoolExecutor在包java.util.concurrent下
ThreadPoolExecutor threadPool= new ThreadPoolExecutor(10, 15, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
第一个参数10 表示这个线程池初始化了10个线程在里面工作
第二个参数15 表示如果10个线程不够用了,就会自动增加到最多15个线程
第三个参数60 结合第四个参数TimeUnit.SECONDS,表示经过60秒,多出来的线程还没有接到活儿,就会回收,最后保持池子里就10个
第四个参数TimeUnit.SECONDS 如上
第五个参数 new LinkedBlockingQueue() 用来放任务的集合
execute方法用于添加新的任务
threadPool.execute(new Runnable(){});
线程的关闭和开启很浪费资源,所以可以采用线程池,用完的线程没有结束等待下一个任务的到来。
自定义线程池:
package multiplethread; import java.util.LinkedList; public class ThreadPool { // 线程池大小 int threadPoolSize; // 任务容器 LinkedList<Runnable> tasks = new LinkedList<Runnable>(); // 试图消费任务的线程 public ThreadPool() { threadPoolSize = 10; // 启动10个任务消费者线程 synchronized (tasks) { for (int i = 0; i < threadPoolSize; i++) { new TaskConsumeThread("任务消费者线程 " + i).start(); } } } public void add(Runnable r) { synchronized (tasks) { tasks.add(r); // 唤醒等待的任务消费者线程 tasks.notifyAll(); } } class TaskConsumeThread extends Thread { public TaskConsumeThread(String name) { super(name); } Runnable task; public void run() { System.out.println("启动: " + this.getName()); while (true) { synchronized (tasks) { while (tasks.isEmpty()) { try { tasks.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } task = tasks.removeLast(); // 允许添加任务的线程可以继续添加任务 tasks.notifyAll(); } System.out.println(this.getName() + " 获取到任务,并执行"); task.run(); } } } }package multiplethread;public class TestThread { public static void main(String[] args) { ThreadPool pool = new ThreadPool(); for (int i = 0; i < 5; i++) { Runnable task = new Runnable() { @Override public void run() { //System.out.println("执行任务"); //任务可能是打印一句话 //可能是访问文件 //可能是做排序 } }; pool.add(task); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

浙公网安备 33010602011771号