package com.file.util;
import java.util.LinkedList;
class ThreadPool extends ThreadGroup {// 继承线程组实现线程池功能
private boolean isClosed = false; // 线程池是否关闭
private LinkedList taskQueue; // 工作任务队列
private static int threadPool_ID = 1; // 线程池的编号
private class TaskThread extends Thread {// 负责从工作队列中取出任务并执行的内部类
private int id;// 任务编号
public TaskThread(int id) {// 构造方法进行初始化
super(ThreadPool.this, id + "");// 将线程加入到当前线程组中
this.id = id;
}
public void run() {
while (!isInterrupted()) { // isInterrupted()方法继承自Thread类,判断线程是否被中断
Runnable task = null;
task = getTask(id); // 取出任务
// 如果getTask()返回null或者线程执行getTask()时被中断,则结束此线程
if (task == null)
return;
try {
task.run(); // 运行任务
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
public ThreadPool(int poolSize) { // 构造方法传入线程池中的工作线程的数量
super(threadPool_ID + ""); // 指定线程组名称
setDaemon(true); // 继承线程组的方法用来设置是否守护线程池
taskQueue = new LinkedList();// 创建工作任务队列
for (int i = 0; i < poolSize; i++) {// 循环创建任务线程
new TaskThread(i).start();// 创建并启动任务线程,根据线程池数据创建任务线程
}
}
public synchronized void executeTask(Runnable task) {// 添加新任务并执行任务
if (isClosed) {// 判断标识
throw new IllegalStateException();// 抛出不合理状态异常
}
if (task != null) {
taskQueue.add(task);// 向任务队列中加入一个任务
notify(); // 唤醒待任务的工作任务线程
}
}
private synchronized Runnable getTask(int id) {// 取出任务
try {
while (taskQueue.size() == 0) {// 循环使线程等待任务
if (isClosed)
return null;
System.out.println("工作线程" + id + "等待任务...");
wait(); // 如果任务队列中没有任务,就等待任务
}
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("等待任务出现错误:" + e.getMessage());
}
System.out.println("工作线程" + id + "开始执行任务...");
return (Runnable) taskQueue.removeFirst(); // 返回第一个任务并从队列中删除
}
public synchronized void closeThreadPool() {// 关闭线程池
if (!isClosed) {// 判断标识
waitTaskFinish();// 等待任务线程执行完毕
isClosed = true;// 标识为真
taskQueue.clear();// 任务队列清空
interrupt(); // 唤醒线程池中的所有的工作线程
}
}
public void waitTaskFinish() {// 等待任务线程把所有任务执行完毕
synchronized (this) {
isClosed = true;// 标识为真
notifyAll();// 唤醒待任务的工作任务线程
}
Thread[] threads = new Thread[activeCount()]; // 创建线程组中活动的线程组
int count = enumerate(threads); // 根据活动的线程获得线程组中当前所有活动的工作线程
for (int i = 0; i < count; i++) { // 循环等待所有工作线程结束
try {
threads[i].join();// 等待工作线程结束
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("任务执行出错:" + e.getMessage());
}
}
}
}
public class TextThreadPool {// 操作线程池执行任务的类
private static Runnable createTask(final int taskID) {// 创建任务方法
return new Runnable() {
public void run() {// 创建任务
System.out.println("任务开始,编号为" + taskID);
System.out.println("start task");
System.out.println("任务结束,编号为" + taskID);
}
};
}
public static void main(String[] args) {// java程序主入口处
ThreadPool threadPool = new ThreadPool(3); // 创建一个有个3任务线程的线程池
try {
Thread.sleep(600);// 休眠600毫秒,让线程池中的任务线程全部运行
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("线程休眠出错:" + e.getMessage());
}
for (int i = 0; i < 5; i++) { // 循环创建并执行任务
threadPool.executeTask(createTask(i));
}
threadPool.waitTaskFinish(); // 等待所有任务执行完毕
threadPool.closeThreadPool(); // 关闭线程池
}
}
import java.util.LinkedList;
class ThreadPool extends ThreadGroup {// 继承线程组实现线程池功能
private boolean isClosed = false; // 线程池是否关闭
private LinkedList taskQueue; // 工作任务队列
private static int threadPool_ID = 1; // 线程池的编号
private class TaskThread extends Thread {// 负责从工作队列中取出任务并执行的内部类
private int id;// 任务编号
public TaskThread(int id) {// 构造方法进行初始化
super(ThreadPool.this, id + "");// 将线程加入到当前线程组中
this.id = id;
}
public void run() {
while (!isInterrupted()) { // isInterrupted()方法继承自Thread类,判断线程是否被中断
Runnable task = null;
task = getTask(id); // 取出任务
// 如果getTask()返回null或者线程执行getTask()时被中断,则结束此线程
if (task == null)
return;
try {
task.run(); // 运行任务
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
public ThreadPool(int poolSize) { // 构造方法传入线程池中的工作线程的数量
super(threadPool_ID + ""); // 指定线程组名称
setDaemon(true); // 继承线程组的方法用来设置是否守护线程池
taskQueue = new LinkedList();// 创建工作任务队列
for (int i = 0; i < poolSize; i++) {// 循环创建任务线程
new TaskThread(i).start();// 创建并启动任务线程,根据线程池数据创建任务线程
}
}
public synchronized void executeTask(Runnable task) {// 添加新任务并执行任务
if (isClosed) {// 判断标识
throw new IllegalStateException();// 抛出不合理状态异常
}
if (task != null) {
taskQueue.add(task);// 向任务队列中加入一个任务
notify(); // 唤醒待任务的工作任务线程
}
}
private synchronized Runnable getTask(int id) {// 取出任务
try {
while (taskQueue.size() == 0) {// 循环使线程等待任务
if (isClosed)
return null;
System.out.println("工作线程" + id + "等待任务...");
wait(); // 如果任务队列中没有任务,就等待任务
}
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("等待任务出现错误:" + e.getMessage());
}
System.out.println("工作线程" + id + "开始执行任务...");
return (Runnable) taskQueue.removeFirst(); // 返回第一个任务并从队列中删除
}
public synchronized void closeThreadPool() {// 关闭线程池
if (!isClosed) {// 判断标识
waitTaskFinish();// 等待任务线程执行完毕
isClosed = true;// 标识为真
taskQueue.clear();// 任务队列清空
interrupt(); // 唤醒线程池中的所有的工作线程
}
}
public void waitTaskFinish() {// 等待任务线程把所有任务执行完毕
synchronized (this) {
isClosed = true;// 标识为真
notifyAll();// 唤醒待任务的工作任务线程
}
Thread[] threads = new Thread[activeCount()]; // 创建线程组中活动的线程组
int count = enumerate(threads); // 根据活动的线程获得线程组中当前所有活动的工作线程
for (int i = 0; i < count; i++) { // 循环等待所有工作线程结束
try {
threads[i].join();// 等待工作线程结束
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("任务执行出错:" + e.getMessage());
}
}
}
}
public class TextThreadPool {// 操作线程池执行任务的类
private static Runnable createTask(final int taskID) {// 创建任务方法
return new Runnable() {
public void run() {// 创建任务
System.out.println("任务开始,编号为" + taskID);
System.out.println("start task");
System.out.println("任务结束,编号为" + taskID);
}
};
}
public static void main(String[] args) {// java程序主入口处
ThreadPool threadPool = new ThreadPool(3); // 创建一个有个3任务线程的线程池
try {
Thread.sleep(600);// 休眠600毫秒,让线程池中的任务线程全部运行
} catch (InterruptedException e) {// 捕获拦截异常
System.out.println("线程休眠出错:" + e.getMessage());
}
for (int i = 0; i < 5; i++) { // 循环创建并执行任务
threadPool.executeTask(createTask(i));
}
threadPool.waitTaskFinish(); // 等待所有任务执行完毕
threadPool.closeThreadPool(); // 关闭线程池
}
}