大家都知道在多线程环境下采用线程池技术,可以降低资源的损耗,那这线程池工作机制是什么?我们可以根据自己想像的定义一个线程池,这里的缺陷是什么
一 ,自定义线程池工具类,需要借助队列来实现
1.首先线程池中线程个数,有默认线程数
2.工作线程组,线程池启动时有默认线程数的线程运行(从阻塞队列中获取去线程并运行)
3.线程任务,用户需要执行的线程任务
4.BlockingQueue阻塞队列
5.线程池需要有execute方法,用来执行于把用户的线程任务放在队列中去
6.销毁线程方法interrupt,并清空队列
public class ThreadPoolDemo { //线程中默认线程的个数 private static int threadCount = 5; //队列中默认任务的个数 private static int queueCount = 100; //工作线程组 private WorkThread[] workThreads; //任务队列,作为一个缓冲 private BlockingQueue<Runnable> taskQueue; //用户在构建线程池的时候,希望启动的线程数 private int work_num; /** * @param work_num 线程池中工作线程的个数 * @param taskCount */ public ThreadPoolDemo(int work_num, int taskCount) { if (work_num <= 0) work_num = threadCount; if (taskCount <= 0) taskCount = queueCount; this.work_num = work_num; taskQueue = new ArrayBlockingQueue<>(taskCount); workThreads = new WorkThread[work_num]; for (int i = 0; i <work_num ; i++) { workThreads[i]=new WorkThread(); workThreads[i].start(); } //Runtime.getRuntime().availableProcessors(); } /** * 执行任务,其实只是把任务加入任务队列,什么时候执行有线程池管理器决定 * @param task */ public void execute(Runnable task){ try { taskQueue.put(task); System.out.println("....线程队列大小................>>>>>>"+taskQueue.size()); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 销毁线程池,该方法保证在所有任务都完成的情况下才销毁所有线程,否则等待任务完成才销毁 */ public void destory(){ System.out.println(".....ready close pool"); for (int i = 0; i < work_num; i++) { WorkThread t = workThreads[i]; if(t.isAlive()){ t.stopWork(); } workThreads[i]=null; } taskQueue.clear();//清空任务队列 System.out.println("线程池销毁/"); } // 覆盖toString方法,返回线程池信息:工作线程个数和已完成任务个数 @Override public String toString() { return "WorkThread number:" + work_num + " wait task number:" + taskQueue.size(); } public int getQueueCount(){ return taskQueue.size(); } private class WorkThread extends Thread { @Override public void run() { Runnable r = null; while (!interrupted()) { try { if(taskQueue.size()>0){ r = taskQueue.take(); if (r != null) { System.out.println("线程......" + r + "......ready exec ........"); r.run(); } r = null;//heap gc } } catch (InterruptedException e) { e.printStackTrace(); } } } public void stopWork(){ interrupt(); } } }
测试类
public class PoolTest { public static void main(String[] args) { ThreadPoolDemo pool = new ThreadPoolDemo(3,0); MyTask t = new MyTask("test thread A"); pool.execute(new MyTask("test thread A")); pool.execute(new MyTask("test thread B")); pool.execute(new MyTask("test thread C")); pool.execute(new MyTask("test thread D")); pool.execute(new MyTask("test thread E")); System.out.println(pool); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } int queueCount = pool.getQueueCount(); if(queueCount==0){ pool.destory(); } System.out.println(pool); } public static class MyTask extends Thread { private String name; public MyTask(String name) { this.name = name; } @Override public String toString() { return this.name; } @Override public void run() { try { Thread.currentThread().setName(name); Thread.sleep(new Random().nextInt(1000)+2000 ); } catch (InterruptedException e) { System.out.println("任务....." + name + ".......sleep InterruptedException:" + Thread.currentThread().isInterrupted()); //Thread.currentThread().interrupt(); } System.out.println("任务....." + name + ".....完成"); } } }
运行结果: