socket编程3-ThreadPool

package Chapter3;

import java.util.LinkedList;

public class ThreadPool extends ThreadGroup {
    private boolean isClosed = false; // 线程池是否关闭
    private LinkedList<Runnable> workQueue; // 表示工作队列,用来存放线程池要执行的任务,每个任务都是Runnable实例
    private static int threadPoolID; // 表示线程池ID
    private int threadID; // 表示工作线程ID

    public ThreadPool(int poolSize) { // poolSize指定线程池中的工作线程数目
        super("TheadPool-" + (threadPoolID++));
        setDaemon(true);
        workQueue = new LinkedList<Runnable>(); // 创建工作队列
        for (int i = 0; i < poolSize; i++)
            new WorkThread().start(); // 创建并启动工作线程
    }

    /** 向工作队列中加入一个新的任务,由工作线程去执行该任务 */
    public synchronized void execute(Runnable task) {
        if (isClosed) {// 线程池被关闭则抛出IllegalStateException异常
            throw new IllegalStateException();
        }
        if (task != null) {
            workQueue.add(task);
            notify();// 唤醒正在getTask()方法中等待的工作线程
        }
    }

    /** 从工作队列中取出一个任务,工作线程会调用此方法 */
    protected synchronized Runnable getTask() throws InterruptedException {
        while (workQueue.size() == 0) {
            if (isClosed)
                return null;
            wait();// 如果工作队列中没有任务,就等待任务
        }
        return workQueue.removeFirst();
    }

    /** 关闭线程池 */
    public synchronized void close() {
        if (!isClosed) {
            isClosed = true;
            workQueue.clear();// 清空工作队列
            interrupt();// 中断所有的工作线程,该方法继承自ThreadGroup类
        }
    }

    /** 等待工作线程把所有任务执行完 */
    public void join() {
        synchronized (this) {
            isClosed = true;
            notifyAll();// 唤醒还在getTask()方法中等待任务的动作线程
        }
        Thread[] threads = new Thread[activeCount()];
        // enumerate()方法继承自ThreadGroup类,获得线程组中当前所有活着的工作线程
        int count = enumerate(threads);
        for (int i = 0; i < count; i++) {// 等待所有工作线程运行结束
            try {
                threads[i].join();// 等待工作线程运行结束
            } catch (InterruptedException ex) {
            }

        }

    }

    /** 内部类:工作线程 */
    private class WorkThread extends Thread {
        public WorkThread() {
            // 加入当前ThreadPool线程组中
            super(ThreadPool.this, "WorkThread-" + (threadID++));
        }

        public void run() {
            while (!isInterrupted()) {// isInterrupted()方法继承自Thread类,判断线程是否被中断
                Runnable task = null;
                try {// 取出任务
                    task = getTask();
                } catch (InterruptedException ex) {
                }
                /**
                 * 如果getTask()返回null或线程执行getTask()时被中断,则结束此线程
                 */
                if (task == null)
                    return;
                try {// 运行任务,异常在catch代码块中捕获
                    task.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    }
}

转载于:https://www.cnblogs.com/stay-sober/p/4158807.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值