Java线程_线程调度之线程池(一)(二)

2014-11-24 03:05:47 · 作者: · 浏览: 5
try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString(){ return this.command; } }

STEP 3.新建监视器线程类,用于定时打印当前线程池的状态

package com.wly.javathread.threadpool;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 一个用于监视ThreadPoolExecutor当前状态的线程
 * @author wly
 *
 */
public class MyMonitorThread implements Runnable {
    private ThreadPoolExecutor executor;

    private int seconds;

    private boolean run=true;

    public MyMonitorThread(ThreadPoolExecutor executor, int delay) {
        this.executor = executor;
        this.seconds=delay;
    }

    public void shutdown() {
        this.run=false;
    }

    @Override
    public void run() {
        while(run) {
                System.out.println(
                    String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                        this.executor.getPoolSize(),
                        this.executor.getCorePoolSize(),
                        this.executor.getActiveCount(),
                        this.executor.getCompletedTaskCount(),
                        this.executor.getTaskCount(),
                        this.executor.isShutdown(),
                        this.executor.isTerminated()));
                try {
                    Thread.sleep(seconds*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }
}

STEP 4.测试线程池类WorkerPool

package com.wly.javathread.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WorkerPool {

    public static void main(String args[]) throws InterruptedException{
        RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
        ThreadFactory threadFactory = Executors.defaultThreadFactory();


        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS
        		, new ArrayBlockingQueue
  
   (2), threadFactory, rejectionHandler);
        MyMonitorThread monitor = new MyMonitorThread(executorPool,3);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        for(int i=0; i<10; i++){
        	System.out.println("--add task.cmd" + i + "--");
            executorPool.execute(new WorkerThread("cmd"+i));
            Thread.sleep(100);
        }

        Thread.sleep(30000);
        //关闭线程池
        executorPool.shutdown();
        Thread.sleep(5000);
        //关闭监视线程
        monitor.shutdown();
    }
}
  

STEP 5. 运行结果:

--add task.cmd0--
pool-1-thread-1 Start. Command = cmd0
[monitor] [1/2] Active: 1, Completed: 0, Task: 1, isShutdown: false, isTerminated: false
--add task.cmd1--
pool-1-thread-2 Start. Command = cmd1
--add task.cmd2--
--add task.cmd3--
--add task.cmd4--
pool-1-thread-3 Start. Command = cmd4
--add task.cmd5--
pool-1-thread-4 Start. Command = cmd5
--add task.cmd6--
cmd6 is rejected
--add task.cmd7--
cmd7 is rejected
--add task.cmd8--
cmd8 is rejected
--add task.cmd9--
cmd9 is rejected
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-1 Start. Command = cmd2
pool-1-thread-2 End.
pool-1-thread-2 Start. Command = cmd3
pool-1-thread-3 End.
pool-1-thread-4 End.
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-2 End.
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: f