📄 threadpool.java
字号:
package de.spieleck.util;
import java.util.Map;
import java.util.HashMap;
/**
* Implement a thread pool for dispatching arbitrary many jobs
* to a fixed number of workers. The tasks must implement
* {@link java.lang.Runnable} and are provided by a {@link TaskSource}.
*
* @author fsn
* @version 0.0
*/
public class ThreadPool
extends ThreadGroup
{
/** How many tasks are still running? */
protected int runningTasks;
/** How many tasks are still running? */
protected int critical = 0;
/** Do we have special exception handlers? */
protected Map eHandlers;
/** Lock for counting */
private Object cLock = new Object();
/** The actual source of work */
protected TaskSource tSource;
/**
* Construct a pool with a default number of threads.
*/
public ThreadPool(TaskSource tSource)
{
this(5, tSource);
}
/**
* Construct a pool with a choosable number of threads.
*/
public ThreadPool(int size, TaskSource tSource)
{
super("ThreadPool");
this.tSource = tSource;
setDaemon(true);
// Of course is is 0, but every starting thread runs into the lock.
String nameBase = toString()+".";
runningTasks = size;
for(int i = 0; i< size; i++ )
{
Thread th = new WorkerThread(this,nameBase+Integer.toString(i));
th.start();
}
}
/**
* Fetch the next assignment.
*/
protected Runnable getTask()
{
try
{
Runnable r;
synchronized(cLock)
{
runningTasks--;
// This is the place where we can check for remaining
// work in a completely synchronized fashion.
// Only source of error: A new job incoming after this
// check has taken place.
if ( !tSource.isWorkLeft() && runningTasks == 0 )
cLock.notifyAll();
critical++;
}
Thread.yield();
r = tSource.getTask();
synchronized(cLock)
{
critical--;
if ( r != null )
runningTasks++;
}
return r;
}
catch(InterruptedException e)
{
return null;
}
}
/**
* Protected attach a new Exception handler.
*/
public synchronized void addExceptionHandler(Class e,
ExceptionHandler handler)
{
if ( eHandlers == null )
eHandlers = new HashMap();
eHandlers.put(e, handler);
}
/**
* Wait until we have started doing something.
*/
public void waitStart()
throws InterruptedException
{
tSource.waitStart();
}
/**
* Wait until we have started doing something.
*/
public void waitDone()
throws InterruptedException
{
for(int i = 0; i < critical; i++)
Thread.yield();
synchronized(cLock)
{
while ( runningTasks > 0 || tSource.isWorkLeft() )
{
cLock.wait();
}
}
}
/**
* Wait until all jobs are done.
* That is, we have to wait for both, that every single task
* is done and that there are no tasks remaining any more.
* But in the very beginning we assume there is something to do and
* wait for at least one task!
*/
public void join()
{
try
{
waitStart();
waitDone();
}
catch ( InterruptedException e )
{
// cannot cope with this
}
}
/**
* How many tasks are still working
*/
public int getRunningTasks()
{
synchronized(cLock)
{
return runningTasks;
}
}
/**
* customizable exception handling.
*/
public void uncaughtException(Thread t, Throwable e)
{
ExceptionHandler eh = null;
if ( eHandlers != null )
eh = (ExceptionHandler) eHandlers.get(e.getClass());
if ( eh != null )
eh.uncaughtException(t, e);
else
super.uncaughtException(t,e);
}
/**
* Do something useful on finalization time
*/
protected void finalize()
throws Throwable
{
interrupt();
destroy();
super.finalize();
}
/**
* Inner class to actually do something.
*/
protected class WorkerThread
extends Thread
{
public WorkerThread(ThreadGroup tg, String name)
{
super(tg, name);
}
public void run()
{
Runnable task;
while ( true )
{
task = getTask();
if ( task == null )
break;
try
{
task.run();
}
catch(Throwable t)
{
uncaughtException(Thread.currentThread(), t);
}
}
}
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// 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.
//
// 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.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -