📄 threadpool.java
字号:
/* * CVS identifier: * * $Id: ThreadPool.java,v 1.8 2000/09/21 16:13:12 dsanta Exp $ * * Class: ThreadPool * * Description: A pool of threads * * * * COPYRIGHT: * * This software module was originally developed by Rapha雔 Grosbois and * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel * Askel鰂 (Ericsson Radio Systems AB); and Bertrand Berthelot, David * Bouchard, F閘ix Henry, Gerard Mozelle and Patrice Onno (Canon Research * Centre France S.A) in the course of development of the JPEG2000 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This * software module is an implementation of a part of the JPEG 2000 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio * Systems AB and Canon Research Centre France S.A (collectively JJ2000 * Partners) agree not to assert against ISO/IEC and users of the JPEG * 2000 Standard (Users) any of their rights under the copyright, not * including other intellectual property rights, for this software module * with respect to the usage by ISO/IEC and Users of this software module * or modifications thereof for use in hardware or software products * claiming conformance to the JPEG 2000 Standard. Those intending to use * this software module in hardware or software products are advised that * their use may infringe existing patents. The original developers of * this software module, JJ2000 Partners and ISO/IEC assume no liability * for use of this software module or modifications thereof. No license * or right to this software module is granted for non JPEG 2000 Standard * conforming products. JJ2000 Partners have full right to use this * software module for his/her own purpose, assign or donate this * software module to any third party and to inhibit third parties from * using this software module for non JPEG 2000 Standard conforming * products. This copyright notice must be included in all copies or * derivative works of this software module. * * Copyright (c) 1999/2000 JJ2000 Partners. * * * */package jj2000.j2k.util;/** * This class implements a thread pool. The thread pool contains a set of * threads which can be given work to do. * * <P>If the Java Virtual Machine (JVM) uses native threads, then the * different threads will be able to execute in different processors in * parallel on multiprocessors machines. However, under some JVMs and * operating systems using native threads is not sufficient to allow the JVM * access to multiple processors. This is the case when native threads are * implemented using POSIX threads on lightweight processes * (i.e. PTHREAD_SCOPE_PROCESS sopce scheduling), which is the case on most * UNIX operating systems. In order to do provide access to multiple * processors it is necessary to set the concurrency level to the number of * processors or slightly higher. This can be achieved by setting the Java * system property with the name defined by CONCURRENCY_PROP_NAME to some * non-negative number. This will make use of the 'NativeServices' class and * supporting native libraries. See 'NativeServices' for details. See * 'CONCURRENCY_PROP_NAME' for the name of the property. * * <P>Initially the thread pool contains a user specified number of idle * threads. Idle threads can be given a target which is run. While running the * target the thread temporarily leaves the idle list. When the target * finishes, it joins the idle list again, waiting for a new target. When a * target is finished a thread can be notified on a particular object that is * given as a lock. * * <P>Jobs can be submitted using Runnable interfaces, using the 'runTarget()' * methods. When the job is submitted, an idle thread will be obtained, the * 'run()' method of the 'Runnable' interface will be executed and when it * completes the thread will be returned to the idle list. In general the * 'run()' method should complete in a rather short time, so that the threds * of the pool are not starved. * * <P>If using the non-asynchronous calls to 'runTarget()', it is important * that any target's 'run()' method, or any method called from it, does not * use non-asynchronous calls to 'runTarget()' on the same thread pool where * it was started. Otherwise this could create a dead-lock when there are not * enough idle threads. * * <P>The pool also has a global error and runtime exception condition (one * for 'Error' and one for 'RuntimeException'). If a target's 'run()' method * throws an 'Error' or 'RuntimeException' the corresponding exception * condition is set and the exception object saved. In any subsequent call to * 'checkTargetErrors()' the saved exception object is thrown. Likewise, if a * target's 'run()' method throws any other subclass of 'Throwable' a new * 'RuntimeException' is created and saved. It will be thrown on a subsequent * call to 'checkTargetErrors()'. If more than one exception occurs between * calls to 'checkTargetErrors()' only the last one is saved. Any 'Error' * condition has precedence on all 'RuntimeException' conditions. The threads * in the pool are unaffected by any exceptions thrown by targets. * * <P>The only exception to the above is the 'ThreadDeath' exception. If a * target's 'run()' method throws the 'ThreadDeath' exception a warning * message is printed and the exception is propagated, which will terminate * the thread in which it occurs. This could lead to instabilities of the * pool. The 'ThreadDeath' exception should never be thrown by the program. It * is thrown by the Java(TM) Virtual Machine when Thread.stop() is * called. This method is deprecated and should never be called. * * <P>All the threads in the pool are "daemon" threads and will automatically * terminate when no daemon threads are running. * * @see NativeServices * * @see #CONCURRENCY_PROP_NAME * * @see Runnable * * @see Thread * * @see Error * * @see RuntimeException * * */public class ThreadPool { /** The name of the property that sets the concurrency level: jj2000.j2k.util.ThreadPool.concurrency */ public final static String CONCURRENCY_PROP_NAME = "jj2000.j2k.util.ThreadPool.concurrency"; /** The array of idle threads and the lock for the manipulation of the * idle thread list. */ private ThreadPoolThread idle[]; /** The number of idle threads */ private int nidle; /** The name of the pool */ private String poolName; /** The priority for the pool */ private int poolPriority; /** The last error thrown by a target. Null if none */ // NOTE: needs to be volatile, so that only one copy exits in memory private volatile Error targetE; /** The last runtime exception thrown by a target. Null if none */ // NOTE: needs to be volatile, so that only one copy exits in memory private volatile RuntimeException targetRE; /** * The threads that are managed by the pool. * */ class ThreadPoolThread extends Thread { private Runnable target; private Object lock; private boolean doNotifyAll; /** * Creates a ThreadPoolThread object, setting its name according to * the given 'idx', daemon type and the priority to the one of the * pool. * * @param idx The index of this thread in the pool * * @param name The name of the thread * */ public ThreadPoolThread(int idx, String name) { super(name); setDaemon(true); setPriority(poolPriority); } /** * The method that is run by the thread. This method first joins the * idle state in the pool and then enters an infinite loop. In this * loop it waits until a target to run exists and runs it. Once the * target's run() method is done it re-joins the idle state and * notifies the waiting lock object, if one exists. * * <P>An interrupt on this thread has no effect other than forcing a * check on the target. Normally the target is checked every time the * thread is woken up by notify, no interrupts should be done. * * <P>Any exception thrown by the target's 'run()' method is catched * and this thread is not affected, except for 'ThreadDeath'. If a * 'ThreadDeath' exception is catched a warning message is printed by * the 'FacilityManager' and the exception is propagated up. For * exceptions which are subclasses of 'Error' or 'RuntimeException' * the corresponding error condition is set and this thread is not * affected. For any other exceptions a new 'RuntimeException' is * created and the error condition is set, this thread is not affected. * */ public void run() { // Join the idle threads list putInIdleList(this); // Permanently lock the object while running so that target can // not be changed until we are waiting again. While waiting for a // target the lock is released. synchronized (this) { while (true) { // Wait until we get a target while (target == null) { try { this.wait(); } catch (InterruptedException e) { } } // Run the target and catch all possible errors try { target.run(); } catch (ThreadDeath td) { // We have been instructed to abruptly terminate // the thread, which should never be done. This can // cause another thread, or the system, to lock. FacilityManager.getMsgLogger(). printmsg(MsgLogger.WARNING, "Thread.stop() called on a ThreadPool "+ "thread or ThreadDeath thrown. This is "+ "deprecated. Lock-up might occur."); throw td; } catch (Error e) { targetE = e; } catch (RuntimeException re) { targetRE = re; } catch (Throwable ue) { // A totally unexpected error has occurred // (Thread.stop(Throwable) has been used, which should // never be. targetRE = new RuntimeException("Unchecked exception "+ "thrown by target's "+ "run() method in pool "+ poolName+"."); } // Join idle threads putInIdleList(this); // Release the target and notify lock (i.e. wakeup) target = null; if (lock != null) { synchronized (lock) { if (doNotifyAll) { lock.notifyAll(); } else { lock.notify(); } } } } } } /** * Assigns a target to this thread, with an optional notify lock and a * notify mode. The another target is currently running the method * will block until it terminates. After setting the new target the * runner thread will be wakenup and execytion will start. * * @param target The runnable object containing the 'run()' method to * run. * * @param lock An object on which notify will be called once the * target's run method has finished. A thread to be notified should be * waiting on that object. If null no thread is notified. * * @param notifyAll If true 'notifyAll()', instead of 'notify()', will * be called on tghe lock. * */ synchronized void setTarget(Runnable target, Object lock, boolean notifyAll) { // Set the target this.target = target; this.lock = lock; doNotifyAll = notifyAll; // Wakeup the thread this.notify(); } } /** * Creates a new thread pool of the given size, thread priority and pool * name. * * <P>If the Java system property of the name defined by * 'CONCURRENCY_PROP_NAME' is set, then an attempt will be made to load * the library that supports concurrency setting (see * 'NativeServices'). If that succeds the concurrency level will be set to * the specified value. Otherwise a warning is printed. * * @param size The size of the pool (number of threads to create in the * pool).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -