threadpool.java

来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 147 行

JAVA
147
字号
package de.fhm.jkf.thread.cl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Theodor Willax
 *
 * <br><br><center><table border="1" width="80%"><hr>
 * <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
 * <p>
 * Copyright (C) 2002 by Theodor Willax
 * <p>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * <p>
 * You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
 * GNU Lesser General Public License</a> along with this library; if not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 * <hr></table></center>
 * 
 * This <code>ThreadPool</code> creates at most a predetermined
 * number of <code>ServerThreads</code>. Are all threads busy, a
 * task gets into the waiting list. Corresponding the <code>ThreadPool</code>
 * maintains a list of idle threads, a list of busy threads and a queue of
 * tasks waiting for processing.
 */
public class ThreadPool implements TaskReadyListener {
	/**
	 * <code>List</code> of available idle threads.
	 */
	private List idleThreads = new ArrayList();
	/**
	 * <code>List</code> of busy threads.
	 */
	private List busyThreads = new ArrayList();
	/**
	 * <code>List</code> of tasks which haven't been done yet.
	 */
	private List queue = new ArrayList();
	private boolean isStopped = false;
	public static final int MAX_THREADS = 5;

	/**
	* Creates a new <code>ThreadPool</code> with the default number
	* of threads.
	*/
	public ThreadPool() {
		this(MAX_THREADS);
	}

	/**
	* Creates a new <code>ThreadPool</code> with the given number of
	* threads.
	* 
	* @param max maximum number of threads in this pool.
	*/
	public ThreadPool(int max) {
		ServerThread s = null;
		for (int i = 0; i < max; i++) {
			s = new ServerThread("JKFServerThread-" + i);
			idleThreads.add(s);
			s.addTaskReadyListener(this);
			s.start();
		}
	}

	/**
	 * Gives a task to the <code>ThreadPool</code> for processing. If
	 * no thread is free, the task gets into the queue for tasks to do.
	 * 
	 * @param t task for processing
	 * @exception IllegalStateException if the <code>ThreadPool</code>
	 * was stopped.
	 */
	public synchronized void doTask(InterruptableTask t) {
		if (isStopped == true) {
			throw new IllegalStateException("TreadPool was stopped!");
		}
		if (!idleThreads.isEmpty()) {
			ServerThread s = (ServerThread) idleThreads.get(0);
			idleThreads.remove(s);
			busyThreads.add(s);
			s.runTask(t);
		} else {
			queue.add(t);
		}
	}

	/**
	 * Interrupts <b>all</b> tasks an clears the queue of tasks waiting for
	 * processing. The pool is not stopped. You can give new tasks to it.
	 */
	public synchronized void interrupt() {
		for (Iterator it = queue.iterator(); it.hasNext();) {
			 ((InterruptableTask) it.next()).interrupt();
		}
		queue.clear();
		for (Iterator it = busyThreads.iterator(); it.hasNext();) {
			 ((ServerThread) it.next()).interruptTask();
		}
	}

	/**
	 * Stops all currently running tasks and all idle tasks (if there are any).
	 * Clears the list of idle threads. The pool is unable to process any new
	 * tasks!
	 */
	public void stop() {
		isStopped = true;
		interrupt();
		for (Iterator it = idleThreads.iterator(); it.hasNext();)
			 ((ServerThread) it.next()).interruptTask();
		idleThreads.clear();
	}

	/**
	 * If a thread has processing of a task finished, he gets either interrupted (after
	 * a previous call to <cod>stop</code>) or the gets a task from the queue or he
	 * gets into the list of idle threads.
	 * 
	 * @param s reference to the tread which finished processing.
	 */
	public synchronized void taskReady(ServerThread s) {
		if (isStopped) {
			s.interrupt();
			busyThreads.remove(s);
		} else if (!queue.isEmpty()) {
			InterruptableTask t = (InterruptableTask) queue.get(0);
			queue.remove(t);
			s.runTask(t);
		} else {
			idleThreads.add(s);
			busyThreads.remove(s);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?