スレッド・プール(スレッド数がスレッド・プールの容量を超えた場合、メッセージ・プロンプト・ユーザーに戻る)

3795 ワード

package sample;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/*
 * 
 * Executors     ExecutorService        ThreadPoolExecutor      ,
 *   ThreadPoolExecutor           。
 *          ThreadPoolExecutor          ,            ,
 *          RejectedExecutionHandler                   。
 * 
 */

public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {
    /**
     * ThreadPoolExecutor        ,        、     、
     *          。  ,                    。
     */
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        System.out.println(r.toString() + " is rejected");
    }

}
package sample;

import java.util.concurrent.ThreadPoolExecutor;

public class MonitorThread implements Runnable {
    private ThreadPoolExecutor executor;

    private int seconds;

    private boolean run = true;

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

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

    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();
            }
        }

    }
}
package sample;
 
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
public class WorkerPoolAdvanced {
 
    public static void main(String args[]) throws InterruptedException{
        //RejectedExecutionHandler implementation
        RejectedExecutionHandler rejectionHandler = new RejectedExecutionHandlerImpl();
        //Get the ThreadFactory implementation to use
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //creating the ThreadPoolExecutor
        /**
         *    :     ThreadPoolExecutor  ,         2、     4、        2。
         *   ,     4                 ,
         *         2           RejectedExecutionHandlerImpl   。
         */
        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory, rejectionHandler);
        //start the monitoring thread
        MonitorThread monitor = new MonitorThread(executorPool, 3);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        //submit work to the thread pool
        for (int i = 1; i < 11; i++) {
            executorPool.execute(new WorkerThread("Worker Thread No: " + i));
        }
 
        Thread.sleep(30000);
        //shut down the pool
        executorPool.shutdown();
        //shut down the monitor thread
        Thread.sleep(5000);
        monitor.shutdown();
 
    }
}