| Method from org.quartz.simpl.SimpleThreadPool Detail: |
public int blockForAvailableThreads() {
synchronized(nextRunnableLock) {
while((availWorkers.size() < 1 || handoffPending) && !isShutdown) {
try {
nextRunnableLock.wait(500);
} catch (InterruptedException ignore) {
}
}
return availWorkers.size();
}
}
|
protected List createWorkerThreads(int count) {
workers = new LinkedList();
for (int i = 1; i< = count; ++i) {
WorkerThread wt = new WorkerThread(this, threadGroup,
getThreadNamePrefix() + "-" + i,
getThreadPriority(),
isMakeThreadsDaemons());
if (isThreadsInheritContextClassLoaderOfInitializingThread()) {
wt.setContextClassLoader(Thread.currentThread()
.getContextClassLoader());
}
workers.add(wt);
}
return workers;
}
|
public Log getLog() {
return log;
}
|
public int getPoolSize() {
return getThreadCount();
}
|
public int getThreadCount() {
return count;
}
|
public String getThreadNamePrefix() {
return threadNamePrefix;
}
|
public int getThreadPriority() {
return prio;
}
|
public void initialize() throws SchedulerConfigException {
if (count < = 0) {
throw new SchedulerConfigException(
"Thread count must be > 0");
}
if (prio < = 0 || prio > 9) {
throw new SchedulerConfigException(
"Thread priority must be > 0 and < = 9");
}
if(isThreadsInheritGroupOfInitializingThread()) {
threadGroup = Thread.currentThread().getThreadGroup();
} else {
// follow the threadGroup tree to the root thread group.
threadGroup = Thread.currentThread().getThreadGroup();
ThreadGroup parent = threadGroup;
while ( !parent.getName().equals("main") ) {
threadGroup = parent;
parent = threadGroup.getParent();
}
threadGroup = new ThreadGroup(parent, "SimpleThreadPool");
if (isMakeThreadsDaemons()) {
threadGroup.setDaemon(true);
}
}
if (isThreadsInheritContextClassLoaderOfInitializingThread()) {
getLog().info(
"Job execution threads will use class loader of thread: "
+ Thread.currentThread().getName());
}
// create the worker threads and start them
Iterator workerThreads = createWorkerThreads(count).iterator();
while(workerThreads.hasNext()) {
WorkerThread wt = (WorkerThread) workerThreads.next();
wt.start();
availWorkers.add(wt);
}
}
|
public boolean isMakeThreadsDaemons() {
return makeThreadsDaemons;
}
|
public boolean isThreadsInheritContextClassLoaderOfInitializingThread() {
return inheritLoader;
}
|
public boolean isThreadsInheritGroupOfInitializingThread() {
return inheritGroup;
}
|
protected void makeAvailable(SimpleThreadPool.WorkerThread wt) {
synchronized(nextRunnableLock) {
if(!isShutdown)
availWorkers.add(wt);
busyWorkers.remove(wt);
nextRunnableLock.notifyAll();
}
}
|
public boolean runInThread(Runnable runnable) {
if (runnable == null) {
return false;
}
synchronized (nextRunnableLock) {
handoffPending = true;
// Wait until a worker thread is available
while ((availWorkers.size() < 1) && !isShutdown) {
try {
nextRunnableLock.wait(500);
} catch (InterruptedException ignore) {
}
}
if (!isShutdown) {
WorkerThread wt = (WorkerThread)availWorkers.removeFirst();
busyWorkers.add(wt);
wt.run(runnable);
}
else {
// If the thread pool is going down, execute the Runnable
// within a new additional worker thread (no thread from the pool).
WorkerThread wt = new WorkerThread(this, threadGroup,
"WorkerThread-LastJob", prio, isMakeThreadsDaemons(), runnable);
busyWorkers.add(wt);
workers.add(wt);
wt.start();
}
nextRunnableLock.notifyAll();
handoffPending = false;
}
return true;
}
Run the given Runnable object in the next available
Thread. If while waiting the thread pool is asked to
shut down, the Runnable is executed immediately within a new additional
thread.
|
public void setMakeThreadsDaemons(boolean makeThreadsDaemons) {
this.makeThreadsDaemons = makeThreadsDaemons;
}
|
public void setThreadCount(int count) {
this.count = count;
}
|
public void setThreadNamePrefix(String prfx) {
this.threadNamePrefix = prfx;
}
|
public void setThreadPriority(int prio) {
this.prio = prio;
}
Set the thread priority of worker threads in the pool - has no effect
after initialize() has been called.
|
public void setThreadsInheritContextClassLoaderOfInitializingThread(boolean inheritLoader) {
this.inheritLoader = inheritLoader;
}
|
public void setThreadsInheritGroupOfInitializingThread(boolean inheritGroup) {
this.inheritGroup = inheritGroup;
}
|
public void shutdown() {
shutdown(true);
}
Terminate any worker threads in this thread group.
Jobs currently in progress will complete.
|
public void shutdown(boolean waitForJobsToComplete) {
synchronized (nextRunnableLock) {
isShutdown = true;
// signal each worker thread to shut down
Iterator workerThreads = workers.iterator();
while(workerThreads.hasNext()) {
WorkerThread wt = (WorkerThread) workerThreads.next();
wt.shutdown();
availWorkers.remove(wt);
}
// Give waiting (wait(1000)) worker threads a chance to shut down.
// Active worker threads will shut down after finishing their
// current job.
nextRunnableLock.notifyAll();
if (waitForJobsToComplete == true) {
// wait for hand-off in runInThread to complete...
while(handoffPending)
try { nextRunnableLock.wait(100); } catch(Throwable t) {}
// Wait until all worker threads are shut down
while (busyWorkers.size() > 0) {
WorkerThread wt = (WorkerThread) busyWorkers.getFirst();
try {
getLog().debug(
"Waiting for thread " + wt.getName()
+ " to shut down");
// note: with waiting infinite time the
// application may appear to 'hang'.
nextRunnableLock.wait(2000);
} catch (InterruptedException ex) {
}
}
int activeCount = threadGroup.activeCount();
if (activeCount > 0) {
getLog().info(
"There are still " + activeCount + " worker threads active."
+ " See javadoc runInThread(Runnable) for a possible explanation");
}
getLog().debug("shutdown complete");
}
}
}
Terminate any worker threads in this thread group.
Jobs currently in progress will complete.
|