Java 多线程(八)——实现简单线程池

1 简单线程池

package com.jtzen9;

import java.util.LinkedList;

public class ThreadPool extends ThreadGroup {
    // 线程池是否关闭
    private boolean isClosed = false;
    // 表示工作队列,即提交给线程池的任务
    private LinkedList<Runnable> workQueue;
    // 表示线程池ID
    private static int threadPoolID;
    // 表示工作线程ID
    private int threadID;

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

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

    /**
     * 从工作队列中取出一个任务,工作线程会调用此方法
     * @return
     * @throws InterruptedException
     */
    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() {
            // isInterrupted()方法继承自Thread类,判断线程是否被中断
            while (!isInterrupted()) {
                Runnable task = null;
                try {
                    // 得到任务
                    task = getTask();
                }catch (InterruptedException ex){}

                // 如果getTask()返回null或者线程执行getTask()时被中断,则结束此线程
                if (task == null)
                    return;

                try {
                    // 运行任务,捕获异常
                    task.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    }
}

2 测试

package com.jtzen9;

public class Main {
    public static void main(String[] args){
        // 创建线程池,该线程池有3个线程
        ThreadPool threadPool = new ThreadPool(3);
        // 创建5个工作任务,并交给线程池执行
        for (int i = 0; i < 5; i++) {
            threadPool.execute(createTask(i));
        }
        threadPool.join();
        threadPool.close();
    }

    /**  定义了一个简单的任务(打印ID)   */
    private static Runnable createTask(final int taskID) {
        return new Runnable() {
            public void run() {
                System.out.println("Task " + taskID + ": start");
                try {
                    Thread.sleep(500);  //增加执行一个任务的时间
                } catch (InterruptedException ex) { }
                System.out.println("Task " + taskID + ": end");
            }
        };
    }
}

3 输出

Task 0: start
Task 1: start
Task 2: start
Task 1: end
Task 0: end
Task 3: start
Task 4: start
Task 2: end
Task 3: end
Task 4: end
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.ljl.org.test4; /** *@DEMO:Interview *@Author:jilongliang *@Date:2013-4-17 * * 分别使用Runnable接口和Thread类编程实 编写一应用程序创建两个线程一个线程打印输出1—1000之间所有的奇数(Odd Number) * 另外一个线程打印输出1-1000之间所有的偶数(Even Number)要求两个线程随机休眠一 段时间后 继续打印输出下一个数 * * 创建线程有两种方式: 1.实现Runnable接口 2.继承Thread类 * 实现方式和继承方式有啥区别? * 实现方式的好处:避免了单继承的局限性 在定义线程时. * 建议使用实现方式 * 区别: * 继承Thread:线程代码存放Thread子类run方法中 实现 * Runnable:线程代码存放接口的子类的run方法 * wait释放资源,释放锁 * sleep释放资源,不释放锁 */ @SuppressWarnings("all") public class Thread1 { public static void main(String[] args) { //方法一 /* OddNumber js = new OddNumber(); js.start(); EvenNumber os = new EvenNumber(); os.start(); while (true) { if (js.i1 == 1000 || os.i2 == 1000) { System.exit(-1); } } */ //方法二 OddNum on=new OddNum(); EvenNum en=new EvenNum(); new Thread(on).start(); new Thread(en).start(); while (true) { if (on.i1 == 1000 || en.i2 == 1000) { System.exit(-1); } } } } /** * ============================继承Thread的线程=============================== */ class EvenNumber extends Thread { int i2; @Override public void run() { for (i2 = 1; i2 <= 1000; i2++) { if (i2 % 2 == 0) { System.out.println("偶數" + i2); } try { sleep((int) (Math.random() * 500) + 500); } catch (Exception e) { } } } } class OddNumber extends Thread { int i1; @Override public void run() { for (i1 = 1; i1 <= 1000; i1++) { if (i1 % 2 != 0) { System.out.println("奇數" + i1); } try { sleep((int) (Math.random() * 500) + 500); } catch (Exception e) { } } } } /** * ============================实现Runnable的线程=============================== */ @SuppressWarnings("all") class OddNum implements Runnable { int i1; @Override public void run() { for (i1 = 1; i1 <= 1000; i1++) { if (i1 % 2 != 0) { System.out.println("奇數" + i1); } try { new Thread().sleep((int) (Math.random() * 500)+500); } catch (Exception e) { } } } } @SuppressWarnings("all") class EvenNum implements Runnable { int i2; @Override public void run() { for (i2 = 1; i2 <= 1000; i2++) { if (i2 % 2 == 0) { System.out.println("偶數" + i2); } try { /**在指定的毫秒数内让当前正在执行的线程休眠 * Math.random()一个小于1的随机数乘于500+500,随眠时间不会超过1000毫秒 */ //new Thread().sleep((int) (Math.random() * 500)+500); new Thread().sleep(1000);//也可以指定特定的参数毫秒 } catch (Exception e) { } } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值