[方案系列文章说明]: 该类型的文章是我在使用和学习中认为不错的解决办法,当然会有更好的方案,也一定会有, 所以文章具有一定的局限性, 请结合自己的思考辩证的看.
线程池的特点
- 迅速响应.
- 线程之间无优先级.
- 线程执行时间短,不阻塞其他任务.
- 线程不可绑定操作,不可被跟踪.
优点
- 对象线程不用重复的创建与销毁,节省时间,资源.
- 可以对线程的数量进行控制.
CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import java.util.LinkedList;
public class ThreadPool extends ThreadGroup {
private boolean isAlive;
private LinkedList<Runnable> taskQueue;
private int threadID;
private static int threadPoolID;
public ThreadPool(int threadNums) { super("线程池-" + (threadPoolID++)); setDaemon(true); isAlive = true; taskQueue = new LinkedList<Runnable>(); for (int i = 0; i < threadNums; i++) { new PooledThread().start(); } }
public synchronized void runTask(Runnable task) { if (!isAlive) { throw new IllegalStateException(); } if (task != null) { taskQueue.add(task); notify(); } }
protected synchronized Runnable getTask() throws InterruptedException { while (taskQueue.size() == 0) { if (!isAlive) { return null; } wait(); } return (Runnable) taskQueue.removeFirst(); }
public synchronized void close() { if (isAlive) { isAlive = false; taskQueue.clear(); interrupt(); } }
public void join() { synchronized (this) { isAlive = false; notifyAll(); } Thread[] threads = new Thread[activeCount()]; int count = enumerate(threads); for (int i = 0; i < count; i++) { try { threads[i].join(); } catch (InterruptedException ei) { ei.printStackTrace(); } } }
public class PooledThread extends Thread {
public PooledThread() { super(ThreadPool.this, "线程(ID号)" + threadID++); }
public void run() { while (!isInterrupted()) { Runnable task = null; try { task = getTask(); } catch (Exception e) { e.printStackTrace(); } if (task == null) { return; } try { System.out.println(this.getId()); task.run(); } catch (Throwable e) { uncaughtException(this, e); } } } } }
|
Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class Test { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } if (args.length != 2) { System.out.println("测试自定义线程池开始"); System.out.println("使用方法,两个参数[任务数,线程数]"); System.out.println("任务数-int:需要执行的任务数"); System.out.println("线程数-int:线程池初始化数量"); } int numTasks = 20; int numThreads = 5; ThreadPool threadPool = new ThreadPool(numThreads); for (int i = 0; i < numTasks; i++) { threadPool.runTask(createTask(i)); } threadPool.join(); }
private static Runnable createTask(final int taskID) { return new Runnable() {
@Override public void run() { System.out.println("任务" + taskID + "开始"); try { Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } System.out.println("任务" + taskID + "结束"); } }; }
}
|