mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Change license to Apache2 and reformat
This commit is contained in:
1 parent
96ce4c0ddf
commit
7aae897def
560 files changed
+73292
-85907
No files matched your search
@@ -1,276 +1,259 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A thread that belongs to a ThreadPool. It will return to the Threadpool when
|
||||
* it has finished its assigned task.
|
||||
*/
|
||||
public class PooledThread implements Runnable {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(PooledThread.class.getName());
|
||||
|
||||
/**
|
||||
* Create the PooledThread.
|
||||
*/
|
||||
protected PooledThread(ThreadPool threadPool, String name, boolean isDaemon,
|
||||
Integer threadPriority) {
|
||||
|
||||
this.name = name;
|
||||
this.threadPool = threadPool;
|
||||
this.lastUsedTime = System.currentTimeMillis();
|
||||
|
||||
thread = new Thread(this, name);
|
||||
thread.setDaemon(isDaemon);
|
||||
|
||||
if (threadPriority != null) {
|
||||
thread.setPriority(threadPriority.intValue());
|
||||
}
|
||||
//thread.start();
|
||||
}
|
||||
|
||||
protected void start() {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign work to this thread. The thread will notify the listener when it
|
||||
* has finished the work.
|
||||
*/
|
||||
public boolean assignWork(Work work) {
|
||||
synchronized (workMonitor) {
|
||||
this.work = work;
|
||||
workMonitor.notifyAll();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* process any assigned work until stopped or interrupted.
|
||||
*/
|
||||
public void run() {
|
||||
// process assigned work until we receive a shutdown signal
|
||||
synchronized (workMonitor) {
|
||||
while (!isStopping) {
|
||||
try {
|
||||
if (work == null) {
|
||||
workMonitor.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
doTheWork();
|
||||
}
|
||||
}
|
||||
|
||||
// Tell stop() we have shut ourselves down successfully
|
||||
synchronized (threadMonitor) {
|
||||
threadMonitor.notifyAll();
|
||||
}
|
||||
//Log.debug("PooledThread [" + getName() + "] finished ");
|
||||
isStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually do the work and gather the appropriate measures.
|
||||
*/
|
||||
private void doTheWork() {
|
||||
if (isStopping){
|
||||
return;
|
||||
}
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
if (work == null) {
|
||||
// probably shutting down the thread
|
||||
|
||||
} else {
|
||||
try {
|
||||
work.setStartTime(startTime);
|
||||
work.getRunnable().run();
|
||||
|
||||
} catch (Throwable ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
|
||||
if (wasInterrupted) {
|
||||
this.isStopping = true;
|
||||
threadPool.removeThread(this);
|
||||
logger.info("PooledThread [" + name + "] removed due to interrupt");
|
||||
try {
|
||||
thread.interrupt();
|
||||
} catch (Exception e){
|
||||
String msg = "Error interrupting PooledThead["+name+"]";
|
||||
logger.log(Level.SEVERE, msg, e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
lastUsedTime = System.currentTimeMillis();
|
||||
totalWorkCount++;
|
||||
totalWorkExecutionTime = totalWorkExecutionTime + lastUsedTime - startTime;
|
||||
this.work = null;
|
||||
threadPool.returnThread(this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to interrupt the thread.
|
||||
* <p>
|
||||
* If the Thread was interrupted then it will be removed from the pool.
|
||||
* </p>
|
||||
*/
|
||||
public void interrupt() {
|
||||
|
||||
// set a flag so doTheWork knows that it was interrupted
|
||||
// and removes rather than returns
|
||||
wasInterrupted = true;
|
||||
try {
|
||||
thread.interrupt();
|
||||
|
||||
} catch (SecurityException ex) {
|
||||
wasInterrupted = false;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the thread has finished.
|
||||
*/
|
||||
public boolean isStopped() {
|
||||
return isStopped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the thread relatively nicely. It will wait a maximum of 10 seconds
|
||||
* for it to complete any existing work.
|
||||
*/
|
||||
protected void stop() {
|
||||
isStopping = true;
|
||||
|
||||
synchronized (threadMonitor) {
|
||||
|
||||
assignWork(null);
|
||||
//trace("stop assigned null work...");
|
||||
try {
|
||||
threadMonitor.wait(10000);
|
||||
} catch (InterruptedException e) {
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
thread = null;
|
||||
threadPool.removeThread(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the name of the thread.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* Returns the currently executing work, otherwise null.
|
||||
*/
|
||||
public Work getWork() {
|
||||
return work;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total number of jobs this thread has run.
|
||||
*/
|
||||
public int getTotalWorkCount() {
|
||||
return totalWorkCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total time for performing all assigned work.
|
||||
*/
|
||||
public long getTotalWorkExecutionTime() {
|
||||
return totalWorkExecutionTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time this thread was last used.
|
||||
*/
|
||||
public long getLastUsedTime() {
|
||||
return lastUsedTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the thread was interrupted.
|
||||
*/
|
||||
private boolean wasInterrupted = false;
|
||||
|
||||
/**
|
||||
* The time the thread was last used.
|
||||
*/
|
||||
private long lastUsedTime;
|
||||
|
||||
/**
|
||||
* The work to run
|
||||
*/
|
||||
private Work work = null;
|
||||
|
||||
/**
|
||||
* Set to indicate the thread is stopping.
|
||||
*/
|
||||
private boolean isStopping = false;
|
||||
|
||||
/**
|
||||
* Set when the thread has stopped.
|
||||
*/
|
||||
private boolean isStopped = false;
|
||||
/**
|
||||
* The background thread.
|
||||
*/
|
||||
private Thread thread = null;
|
||||
|
||||
/**
|
||||
* The pool this worker belongs to.
|
||||
*/
|
||||
private ThreadPool threadPool;
|
||||
|
||||
/**
|
||||
* The name of the Thread
|
||||
*/
|
||||
private String name = null;
|
||||
|
||||
/**
|
||||
* The thread synchronization object.
|
||||
*/
|
||||
private Object threadMonitor = new Object();
|
||||
|
||||
/**
|
||||
* The monitor for work notification.
|
||||
*/
|
||||
private Object workMonitor = new Object();
|
||||
|
||||
/**
|
||||
* Total number of work performed.
|
||||
*/
|
||||
private int totalWorkCount = 0;
|
||||
|
||||
/**
|
||||
* Total work execution time.
|
||||
*/
|
||||
private long totalWorkExecutionTime = 0;
|
||||
|
||||
}
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A thread that belongs to a ThreadPool. It will return to the Threadpool when
|
||||
* it has finished its assigned task.
|
||||
*/
|
||||
public class PooledThread implements Runnable {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(PooledThread.class.getName());
|
||||
|
||||
/**
|
||||
* Create the PooledThread.
|
||||
*/
|
||||
protected PooledThread(ThreadPool threadPool, String name, boolean isDaemon,
|
||||
Integer threadPriority) {
|
||||
|
||||
this.name = name;
|
||||
this.threadPool = threadPool;
|
||||
this.lastUsedTime = System.currentTimeMillis();
|
||||
|
||||
thread = new Thread(this, name);
|
||||
thread.setDaemon(isDaemon);
|
||||
|
||||
if (threadPriority != null) {
|
||||
thread.setPriority(threadPriority.intValue());
|
||||
}
|
||||
//thread.start();
|
||||
}
|
||||
|
||||
protected void start() {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign work to this thread. The thread will notify the listener when it
|
||||
* has finished the work.
|
||||
*/
|
||||
public boolean assignWork(Work work) {
|
||||
synchronized (workMonitor) {
|
||||
this.work = work;
|
||||
workMonitor.notifyAll();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* process any assigned work until stopped or interrupted.
|
||||
*/
|
||||
public void run() {
|
||||
// process assigned work until we receive a shutdown signal
|
||||
synchronized (workMonitor) {
|
||||
while (!isStopping) {
|
||||
try {
|
||||
if (work == null) {
|
||||
workMonitor.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
doTheWork();
|
||||
}
|
||||
}
|
||||
|
||||
// Tell stop() we have shut ourselves down successfully
|
||||
synchronized (threadMonitor) {
|
||||
threadMonitor.notifyAll();
|
||||
}
|
||||
//Log.debug("PooledThread [" + getName() + "] finished ");
|
||||
isStopped = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually do the work and gather the appropriate measures.
|
||||
*/
|
||||
private void doTheWork() {
|
||||
if (isStopping){
|
||||
return;
|
||||
}
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
if (work == null) {
|
||||
// probably shutting down the thread
|
||||
|
||||
} else {
|
||||
try {
|
||||
work.setStartTime(startTime);
|
||||
work.getRunnable().run();
|
||||
|
||||
} catch (Throwable ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
|
||||
if (wasInterrupted) {
|
||||
this.isStopping = true;
|
||||
threadPool.removeThread(this);
|
||||
logger.info("PooledThread [" + name + "] removed due to interrupt");
|
||||
try {
|
||||
thread.interrupt();
|
||||
} catch (Exception e){
|
||||
String msg = "Error interrupting PooledThead["+name+"]";
|
||||
logger.log(Level.SEVERE, msg, e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
lastUsedTime = System.currentTimeMillis();
|
||||
totalWorkCount++;
|
||||
totalWorkExecutionTime = totalWorkExecutionTime + lastUsedTime - startTime;
|
||||
this.work = null;
|
||||
threadPool.returnThread(this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to interrupt the thread.
|
||||
* <p>
|
||||
* If the Thread was interrupted then it will be removed from the pool.
|
||||
* </p>
|
||||
*/
|
||||
public void interrupt() {
|
||||
|
||||
// set a flag so doTheWork knows that it was interrupted
|
||||
// and removes rather than returns
|
||||
wasInterrupted = true;
|
||||
try {
|
||||
thread.interrupt();
|
||||
|
||||
} catch (SecurityException ex) {
|
||||
wasInterrupted = false;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the thread has finished.
|
||||
*/
|
||||
public boolean isStopped() {
|
||||
return isStopped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the thread relatively nicely. It will wait a maximum of 10 seconds
|
||||
* for it to complete any existing work.
|
||||
*/
|
||||
protected void stop() {
|
||||
isStopping = true;
|
||||
|
||||
synchronized (threadMonitor) {
|
||||
|
||||
assignWork(null);
|
||||
//trace("stop assigned null work...");
|
||||
try {
|
||||
threadMonitor.wait(10000);
|
||||
} catch (InterruptedException e) {
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
thread = null;
|
||||
threadPool.removeThread(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* return the name of the thread.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
/**
|
||||
* Returns the currently executing work, otherwise null.
|
||||
*/
|
||||
public Work getWork() {
|
||||
return work;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total number of jobs this thread has run.
|
||||
*/
|
||||
public int getTotalWorkCount() {
|
||||
return totalWorkCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total time for performing all assigned work.
|
||||
*/
|
||||
public long getTotalWorkExecutionTime() {
|
||||
return totalWorkExecutionTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time this thread was last used.
|
||||
*/
|
||||
public long getLastUsedTime() {
|
||||
return lastUsedTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the thread was interrupted.
|
||||
*/
|
||||
private boolean wasInterrupted = false;
|
||||
|
||||
/**
|
||||
* The time the thread was last used.
|
||||
*/
|
||||
private long lastUsedTime;
|
||||
|
||||
/**
|
||||
* The work to run
|
||||
*/
|
||||
private Work work = null;
|
||||
|
||||
/**
|
||||
* Set to indicate the thread is stopping.
|
||||
*/
|
||||
private boolean isStopping = false;
|
||||
|
||||
/**
|
||||
* Set when the thread has stopped.
|
||||
*/
|
||||
private boolean isStopped = false;
|
||||
/**
|
||||
* The background thread.
|
||||
*/
|
||||
private Thread thread = null;
|
||||
|
||||
/**
|
||||
* The pool this worker belongs to.
|
||||
*/
|
||||
private ThreadPool threadPool;
|
||||
|
||||
/**
|
||||
* The name of the Thread
|
||||
*/
|
||||
private String name = null;
|
||||
|
||||
/**
|
||||
* The thread synchronization object.
|
||||
*/
|
||||
private Object threadMonitor = new Object();
|
||||
|
||||
/**
|
||||
* The monitor for work notification.
|
||||
*/
|
||||
private Object workMonitor = new Object();
|
||||
|
||||
/**
|
||||
* Total number of work performed.
|
||||
*/
|
||||
private int totalWorkCount = 0;
|
||||
|
||||
/**
|
||||
* Total work execution time.
|
||||
*/
|
||||
private long totalWorkExecutionTime = 0;
|
||||
|
||||
}
|
||||
@@ -1,489 +1,472 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This is a pool of threads which can be assigned work.
|
||||
* <p>
|
||||
* The Pool will automatically grow as required up to its maximum pool size. The
|
||||
* Pool will be automatically shrink by trimming threads that have been idle for
|
||||
* some time.
|
||||
* </p>
|
||||
*/
|
||||
public class ThreadPool {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ThreadPool.class.getName());
|
||||
|
||||
/**
|
||||
* The max idle time used to trim idle threads from the pool.
|
||||
*/
|
||||
private long maxIdleTime;
|
||||
|
||||
/**
|
||||
* The name of the pool
|
||||
*/
|
||||
private String poolName;
|
||||
|
||||
/**
|
||||
* The initial pool size.
|
||||
*/
|
||||
private int minSize;
|
||||
|
||||
/**
|
||||
* Whether or not the threads are going to be Daemon threads.
|
||||
*/
|
||||
private boolean isDaemon;
|
||||
|
||||
/**
|
||||
* Flag to indicate that the pool is being shutdown.
|
||||
*/
|
||||
private boolean isStopping = false;
|
||||
|
||||
/**
|
||||
* The priority or the threads. Can be null, in which case the threads have
|
||||
* the default priority.
|
||||
*/
|
||||
private Integer threadPriority;
|
||||
|
||||
/**
|
||||
* Incrementing int for thread name. NB: currentThreadCount will go up and
|
||||
* down as the pool grows and shrinks.
|
||||
*/
|
||||
private int uniqueThreadID;
|
||||
|
||||
/**
|
||||
* List of PooledThread that are free for work.
|
||||
*/
|
||||
private Vector<PooledThread> freeList = new Vector<PooledThread>();
|
||||
|
||||
/**
|
||||
* List of PooledThread that are busy.
|
||||
*/
|
||||
private Vector<PooledThread> busyList = new Vector<PooledThread>();
|
||||
|
||||
/**
|
||||
* List holding queued work.
|
||||
*/
|
||||
private Vector<Work> workOverflowQueue = new Vector<Work>();
|
||||
|
||||
/**
|
||||
* The maximum number of threads to grow to. Hitting this limit will have
|
||||
* performance ramifications.
|
||||
*/
|
||||
private int maxSize = 100;
|
||||
|
||||
/**
|
||||
* Flag that the pool should terminate all the threads and stop.
|
||||
*/
|
||||
private boolean stopThePool;
|
||||
|
||||
|
||||
/**
|
||||
* Create the ThreadPool.
|
||||
*/
|
||||
public ThreadPool(String poolName, boolean isDaemon, Integer threadPriority) {
|
||||
|
||||
this.poolName = poolName;
|
||||
this.stopThePool = false;
|
||||
this.isDaemon = isDaemon;
|
||||
this.threadPriority = threadPriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the pool is shutting down.
|
||||
*/
|
||||
public boolean isStopping() {
|
||||
return isStopping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the thread pool.
|
||||
*/
|
||||
public String getName() {
|
||||
return poolName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the minimum size the pool should try to maintain.
|
||||
*/
|
||||
public void setMinSize(int minSize) {
|
||||
if (minSize > 0) {
|
||||
if (minSize > maxSize) {
|
||||
this.maxSize = minSize;
|
||||
}
|
||||
this.minSize = minSize;
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the minimum size the pool should maintain.
|
||||
*/
|
||||
public int getMinSize() {
|
||||
return minSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum size the pool should grow to.
|
||||
*/
|
||||
public void setMaxSize(int maxSize) {
|
||||
if (maxSize > 0) {
|
||||
if (minSize > maxSize) {
|
||||
minSize = maxSize;
|
||||
}
|
||||
this.maxSize = maxSize;
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum size this pool can grow to.
|
||||
*/
|
||||
public int getMaxSize() {
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total number of busy and free threads in the pool.
|
||||
*/
|
||||
public int size() {
|
||||
return busyList.size() + freeList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of currently busy threads.
|
||||
*/
|
||||
public int getBusyCount() {
|
||||
return busyList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a task to the thread pool, specifing the options to wait or queue
|
||||
* the task if the pool is fully busy and can't grow.
|
||||
* <p>
|
||||
* When the pool is fully busy...
|
||||
* </p>
|
||||
* <p>
|
||||
* addToQueue=true -> work is added to queue, returns false<br>
|
||||
* addToQueue=false -> work is not done or queued, returns false<br>
|
||||
* </p>
|
||||
*
|
||||
* @param work the runnable work to do.
|
||||
* @param addToQueueIfFull If the pool is maxed out and this is true then it
|
||||
* queues the Runnable.
|
||||
*/
|
||||
public boolean assign(Runnable work, boolean addToQueueIfFull) {
|
||||
|
||||
if (stopThePool) {
|
||||
throw new RuntimeException("Pool is stopping... no more work please.");
|
||||
}
|
||||
|
||||
Work runWork = new Work(work);
|
||||
|
||||
// get the next available thread in the pool (block)
|
||||
PooledThread thread = getNextAvailableThread();
|
||||
if (thread != null) {
|
||||
// assign the work to that thread
|
||||
busyList.add(thread);
|
||||
thread.assignWork(runWork);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
if (addToQueueIfFull) {
|
||||
runWork.setEnterQueueTime(System.currentTimeMillis());
|
||||
workOverflowQueue.add(runWork);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the thread from the pool. The thread should be stopped before it
|
||||
* is removed.
|
||||
*/
|
||||
protected void removeThread(PooledThread thread) {
|
||||
synchronized (freeList) {
|
||||
busyList.remove(thread);
|
||||
freeList.remove(thread);
|
||||
freeList.notify();
|
||||
|
||||
// if (ThreadPoolManager.getDebugLevel()>0){
|
||||
//Log.debug("PooledThread stopped [" + getName() + "]");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fired when a Thread from the pool has finished, and can be put back into
|
||||
* the pool.
|
||||
*/
|
||||
protected void returnThread(PooledThread thread) {
|
||||
|
||||
synchronized (freeList) {
|
||||
|
||||
// deregister from the busyList
|
||||
busyList.remove(thread);
|
||||
|
||||
if (!workOverflowQueue.isEmpty()) {
|
||||
// get the first bit of work off the queue
|
||||
Work queuedWork = (Work) workOverflowQueue.remove(0);
|
||||
|
||||
// work out the queue time and counts etc
|
||||
queuedWork.setExitQueueTime(System.currentTimeMillis());
|
||||
busyList.add(thread);
|
||||
thread.assignWork(queuedWork);
|
||||
|
||||
} else {
|
||||
// put the thread back onto the available list
|
||||
freeList.add(thread);
|
||||
// tell shutdown() one has returned
|
||||
freeList.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the next available thread. Block until thread is available. NB: The
|
||||
* dispatcher is blocked but work can still be assigned to the dispatcher in
|
||||
* a non-blocking way
|
||||
*/
|
||||
private PooledThread getNextAvailableThread() {
|
||||
|
||||
synchronized (freeList) {
|
||||
if (!freeList.isEmpty()) {
|
||||
return (PooledThread) freeList.remove(0);
|
||||
}
|
||||
if (size() < maxSize) {
|
||||
return growPool(true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Iterator of PooledThread that are currently running. You should
|
||||
* only use this for display. Use the getPooledThread() or interrupt()
|
||||
* methods to interrupt a particular thread.
|
||||
*
|
||||
* @return an Iterator of busy PooledThread's.
|
||||
*/
|
||||
public Iterator<PooledThread> getBusyThreads() {
|
||||
synchronized (freeList) {
|
||||
return busyList.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the threadpool stopping all the threads. This will
|
||||
* wait until any busy threads have finished their assigned work.
|
||||
*/
|
||||
protected void shutdown() {
|
||||
|
||||
synchronized (freeList) {
|
||||
isStopping = true;
|
||||
|
||||
int size = size();
|
||||
|
||||
if (size > 0){
|
||||
String msg = null;
|
||||
msg = "ThreadPool [" + poolName + "] Shutting down; threadCount[" + size()
|
||||
+ "] busyCount[" + getBusyCount() + "]";
|
||||
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
stopThePool = true;
|
||||
|
||||
while (!freeList.isEmpty()) {
|
||||
PooledThread thread = (PooledThread) freeList.remove(0);
|
||||
thread.stop();
|
||||
}
|
||||
|
||||
try {
|
||||
while (getBusyCount() > 0) {
|
||||
// synchronized (freeList) {
|
||||
String msg = "ThreadPool [" + poolName + "] has [" + getBusyCount()
|
||||
+ "] busy threads, waiting for those to finish.";
|
||||
logger.info(msg);
|
||||
|
||||
Iterator<PooledThread> busyThreads = getBusyThreads();
|
||||
while (busyThreads.hasNext()) {
|
||||
PooledThread busyThread = (PooledThread) busyThreads.next();
|
||||
|
||||
String threadName = busyThread.getName();
|
||||
Work work = busyThread.getWork();
|
||||
|
||||
String busymsg = "Busy thread [" + threadName + "] work[" + work + "]";
|
||||
logger.info(busymsg);
|
||||
}
|
||||
// trace("wait for a busy thread to be put back into
|
||||
// freeList");
|
||||
freeList.wait();
|
||||
PooledThread thread = (PooledThread) freeList.remove(0);
|
||||
// trace("wait finished...now shut it down [" +
|
||||
// thread.getName() + "]");
|
||||
if (thread != null) {
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim or grow the pool leaving at least min free.
|
||||
*/
|
||||
protected void maintainPoolSize() {
|
||||
synchronized (freeList) {
|
||||
if (isStopping) {
|
||||
// don't bother as the pool is shutting down
|
||||
return;
|
||||
}
|
||||
|
||||
int numToStop = size() - minSize;
|
||||
if (numToStop > 0) {
|
||||
// should trim idle threads as we are over the minSize
|
||||
long usedAfter = System.currentTimeMillis() - maxIdleTime;
|
||||
ArrayList<PooledThread> stopList = new ArrayList<PooledThread>();
|
||||
Iterator<PooledThread> it = freeList.iterator();
|
||||
while (it.hasNext() && numToStop > 0) {
|
||||
PooledThread thread = (PooledThread) it.next();
|
||||
if (thread.getLastUsedTime() < usedAfter) {
|
||||
stopList.add(thread);
|
||||
numToStop--;
|
||||
}
|
||||
}
|
||||
Iterator<PooledThread> stopIt = stopList.iterator();
|
||||
while (stopIt.hasNext()) {
|
||||
PooledThread thread = (PooledThread) stopIt.next();
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
int numToAdd = minSize - size();
|
||||
if (numToAdd > 0) {
|
||||
// should add some more to the pool
|
||||
for (int i = 0; i < numToAdd; i++) {
|
||||
growPool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interrupt a named thread that is currently busy.
|
||||
* <p>
|
||||
* Returns the thread that was interrupted or null if the thread
|
||||
* was not found. If the thread was interrupted then it will
|
||||
* automatically be stopped and removed from the pool.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note that it may take some time to actually interrupt the thread so
|
||||
* an immediate test to see if the thread stopped will probably be wrong.
|
||||
* <pre><code>
|
||||
* ThreadPool test = ThreadPoolManager.getThreadPool("test");
|
||||
* PooledThread pt = test.interrupt("test.1");
|
||||
* if (pt == null) {
|
||||
* // the thread was not found, perhaps finished?
|
||||
* } else {
|
||||
* // give interrupt a little time to execute
|
||||
* Thread.sleep(1000);
|
||||
* boolean hasStopped = pt.isStopped();
|
||||
* //..
|
||||
* }
|
||||
* </code></pre>
|
||||
* </p>
|
||||
* @return the thread that was interrupted
|
||||
*/
|
||||
public PooledThread interrupt(String threadName) {
|
||||
PooledThread thread = getBusyThread(threadName);
|
||||
if (thread != null) {
|
||||
thread.interrupt();
|
||||
return thread;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a thread using its name from the busy list. Returns null if the
|
||||
* thread is not found in the busy list.
|
||||
*/
|
||||
public PooledThread getBusyThread(String threadName) {
|
||||
synchronized (freeList) {
|
||||
Iterator<PooledThread> it = getBusyThreads();
|
||||
while (it.hasNext()) {
|
||||
PooledThread pt = (PooledThread) it.next();
|
||||
if (pt.getName().equals(threadName)) {
|
||||
return pt;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grow the pool with the option of either putting it on the available list,
|
||||
* or returning it.
|
||||
*/
|
||||
private PooledThread growPool(boolean andReturn) {
|
||||
|
||||
synchronized (freeList) {
|
||||
|
||||
String threadName = poolName + "." + uniqueThreadID++;
|
||||
PooledThread bgw = new PooledThread(this, threadName, isDaemon, threadPriority);
|
||||
bgw.start();
|
||||
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine("ThreadPool grow created [" + threadName + "] size[" + size() + "]");
|
||||
}
|
||||
if (andReturn) {
|
||||
return bgw;
|
||||
} else {
|
||||
freeList.add(bgw);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum amount of time in millis that Threads can be idle
|
||||
* before they are trimmed.
|
||||
*/
|
||||
public long getMaxIdleTime() {
|
||||
return maxIdleTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maxiumium amount of time in millis that Threads can be idle
|
||||
* before they are trimed.
|
||||
*/
|
||||
public void setMaxIdleTime(long maxIdleTime) {
|
||||
this.maxIdleTime = maxIdleTime;
|
||||
}
|
||||
|
||||
}
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Vector;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This is a pool of threads which can be assigned work.
|
||||
* <p>
|
||||
* The Pool will automatically grow as required up to its maximum pool size. The
|
||||
* Pool will be automatically shrink by trimming threads that have been idle for
|
||||
* some time.
|
||||
* </p>
|
||||
*/
|
||||
public class ThreadPool {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ThreadPool.class.getName());
|
||||
|
||||
/**
|
||||
* The max idle time used to trim idle threads from the pool.
|
||||
*/
|
||||
private long maxIdleTime;
|
||||
|
||||
/**
|
||||
* The name of the pool
|
||||
*/
|
||||
private String poolName;
|
||||
|
||||
/**
|
||||
* The initial pool size.
|
||||
*/
|
||||
private int minSize;
|
||||
|
||||
/**
|
||||
* Whether or not the threads are going to be Daemon threads.
|
||||
*/
|
||||
private boolean isDaemon;
|
||||
|
||||
/**
|
||||
* Flag to indicate that the pool is being shutdown.
|
||||
*/
|
||||
private boolean isStopping = false;
|
||||
|
||||
/**
|
||||
* The priority or the threads. Can be null, in which case the threads have
|
||||
* the default priority.
|
||||
*/
|
||||
private Integer threadPriority;
|
||||
|
||||
/**
|
||||
* Incrementing int for thread name. NB: currentThreadCount will go up and
|
||||
* down as the pool grows and shrinks.
|
||||
*/
|
||||
private int uniqueThreadID;
|
||||
|
||||
/**
|
||||
* List of PooledThread that are free for work.
|
||||
*/
|
||||
private Vector<PooledThread> freeList = new Vector<PooledThread>();
|
||||
|
||||
/**
|
||||
* List of PooledThread that are busy.
|
||||
*/
|
||||
private Vector<PooledThread> busyList = new Vector<PooledThread>();
|
||||
|
||||
/**
|
||||
* List holding queued work.
|
||||
*/
|
||||
private Vector<Work> workOverflowQueue = new Vector<Work>();
|
||||
|
||||
/**
|
||||
* The maximum number of threads to grow to. Hitting this limit will have
|
||||
* performance ramifications.
|
||||
*/
|
||||
private int maxSize = 100;
|
||||
|
||||
/**
|
||||
* Flag that the pool should terminate all the threads and stop.
|
||||
*/
|
||||
private boolean stopThePool;
|
||||
|
||||
|
||||
/**
|
||||
* Create the ThreadPool.
|
||||
*/
|
||||
public ThreadPool(String poolName, boolean isDaemon, Integer threadPriority) {
|
||||
|
||||
this.poolName = poolName;
|
||||
this.stopThePool = false;
|
||||
this.isDaemon = isDaemon;
|
||||
this.threadPriority = threadPriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the pool is shutting down.
|
||||
*/
|
||||
public boolean isStopping() {
|
||||
return isStopping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the thread pool.
|
||||
*/
|
||||
public String getName() {
|
||||
return poolName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the minimum size the pool should try to maintain.
|
||||
*/
|
||||
public void setMinSize(int minSize) {
|
||||
if (minSize > 0) {
|
||||
if (minSize > maxSize) {
|
||||
this.maxSize = minSize;
|
||||
}
|
||||
this.minSize = minSize;
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the minimum size the pool should maintain.
|
||||
*/
|
||||
public int getMinSize() {
|
||||
return minSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum size the pool should grow to.
|
||||
*/
|
||||
public void setMaxSize(int maxSize) {
|
||||
if (maxSize > 0) {
|
||||
if (minSize > maxSize) {
|
||||
minSize = maxSize;
|
||||
}
|
||||
this.maxSize = maxSize;
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum size this pool can grow to.
|
||||
*/
|
||||
public int getMaxSize() {
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total number of busy and free threads in the pool.
|
||||
*/
|
||||
public int size() {
|
||||
return busyList.size() + freeList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of currently busy threads.
|
||||
*/
|
||||
public int getBusyCount() {
|
||||
return busyList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign a task to the thread pool, specifing the options to wait or queue
|
||||
* the task if the pool is fully busy and can't grow.
|
||||
* <p>
|
||||
* When the pool is fully busy...
|
||||
* </p>
|
||||
* <p>
|
||||
* addToQueue=true -> work is added to queue, returns false<br>
|
||||
* addToQueue=false -> work is not done or queued, returns false<br>
|
||||
* </p>
|
||||
*
|
||||
* @param work the runnable work to do.
|
||||
* @param addToQueueIfFull If the pool is maxed out and this is true then it
|
||||
* queues the Runnable.
|
||||
*/
|
||||
public boolean assign(Runnable work, boolean addToQueueIfFull) {
|
||||
|
||||
if (stopThePool) {
|
||||
throw new RuntimeException("Pool is stopping... no more work please.");
|
||||
}
|
||||
|
||||
Work runWork = new Work(work);
|
||||
|
||||
// get the next available thread in the pool (block)
|
||||
PooledThread thread = getNextAvailableThread();
|
||||
if (thread != null) {
|
||||
// assign the work to that thread
|
||||
busyList.add(thread);
|
||||
thread.assignWork(runWork);
|
||||
return true;
|
||||
|
||||
} else {
|
||||
if (addToQueueIfFull) {
|
||||
runWork.setEnterQueueTime(System.currentTimeMillis());
|
||||
workOverflowQueue.add(runWork);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the thread from the pool. The thread should be stopped before it
|
||||
* is removed.
|
||||
*/
|
||||
protected void removeThread(PooledThread thread) {
|
||||
synchronized (freeList) {
|
||||
busyList.remove(thread);
|
||||
freeList.remove(thread);
|
||||
freeList.notify();
|
||||
|
||||
// if (ThreadPoolManager.getDebugLevel()>0){
|
||||
//Log.debug("PooledThread stopped [" + getName() + "]");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* fired when a Thread from the pool has finished, and can be put back into
|
||||
* the pool.
|
||||
*/
|
||||
protected void returnThread(PooledThread thread) {
|
||||
|
||||
synchronized (freeList) {
|
||||
|
||||
// deregister from the busyList
|
||||
busyList.remove(thread);
|
||||
|
||||
if (!workOverflowQueue.isEmpty()) {
|
||||
// get the first bit of work off the queue
|
||||
Work queuedWork = (Work) workOverflowQueue.remove(0);
|
||||
|
||||
// work out the queue time and counts etc
|
||||
queuedWork.setExitQueueTime(System.currentTimeMillis());
|
||||
busyList.add(thread);
|
||||
thread.assignWork(queuedWork);
|
||||
|
||||
} else {
|
||||
// put the thread back onto the available list
|
||||
freeList.add(thread);
|
||||
// tell shutdown() one has returned
|
||||
freeList.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the next available thread. Block until thread is available. NB: The
|
||||
* dispatcher is blocked but work can still be assigned to the dispatcher in
|
||||
* a non-blocking way
|
||||
*/
|
||||
private PooledThread getNextAvailableThread() {
|
||||
|
||||
synchronized (freeList) {
|
||||
if (!freeList.isEmpty()) {
|
||||
return (PooledThread) freeList.remove(0);
|
||||
}
|
||||
if (size() < maxSize) {
|
||||
return growPool(true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Iterator of PooledThread that are currently running. You should
|
||||
* only use this for display. Use the getPooledThread() or interrupt()
|
||||
* methods to interrupt a particular thread.
|
||||
*
|
||||
* @return an Iterator of busy PooledThread's.
|
||||
*/
|
||||
public Iterator<PooledThread> getBusyThreads() {
|
||||
synchronized (freeList) {
|
||||
return busyList.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the threadpool stopping all the threads. This will
|
||||
* wait until any busy threads have finished their assigned work.
|
||||
*/
|
||||
protected void shutdown() {
|
||||
|
||||
synchronized (freeList) {
|
||||
isStopping = true;
|
||||
|
||||
int size = size();
|
||||
|
||||
if (size > 0){
|
||||
String msg = null;
|
||||
msg = "ThreadPool [" + poolName + "] Shutting down; threadCount[" + size()
|
||||
+ "] busyCount[" + getBusyCount() + "]";
|
||||
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
stopThePool = true;
|
||||
|
||||
while (!freeList.isEmpty()) {
|
||||
PooledThread thread = (PooledThread) freeList.remove(0);
|
||||
thread.stop();
|
||||
}
|
||||
|
||||
try {
|
||||
while (getBusyCount() > 0) {
|
||||
// synchronized (freeList) {
|
||||
String msg = "ThreadPool [" + poolName + "] has [" + getBusyCount()
|
||||
+ "] busy threads, waiting for those to finish.";
|
||||
logger.info(msg);
|
||||
|
||||
Iterator<PooledThread> busyThreads = getBusyThreads();
|
||||
while (busyThreads.hasNext()) {
|
||||
PooledThread busyThread = (PooledThread) busyThreads.next();
|
||||
|
||||
String threadName = busyThread.getName();
|
||||
Work work = busyThread.getWork();
|
||||
|
||||
String busymsg = "Busy thread [" + threadName + "] work[" + work + "]";
|
||||
logger.info(busymsg);
|
||||
}
|
||||
// trace("wait for a busy thread to be put back into
|
||||
// freeList");
|
||||
freeList.wait();
|
||||
PooledThread thread = (PooledThread) freeList.remove(0);
|
||||
// trace("wait finished...now shut it down [" +
|
||||
// thread.getName() + "]");
|
||||
if (thread != null) {
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim or grow the pool leaving at least min free.
|
||||
*/
|
||||
protected void maintainPoolSize() {
|
||||
synchronized (freeList) {
|
||||
if (isStopping) {
|
||||
// don't bother as the pool is shutting down
|
||||
return;
|
||||
}
|
||||
|
||||
int numToStop = size() - minSize;
|
||||
if (numToStop > 0) {
|
||||
// should trim idle threads as we are over the minSize
|
||||
long usedAfter = System.currentTimeMillis() - maxIdleTime;
|
||||
ArrayList<PooledThread> stopList = new ArrayList<PooledThread>();
|
||||
Iterator<PooledThread> it = freeList.iterator();
|
||||
while (it.hasNext() && numToStop > 0) {
|
||||
PooledThread thread = (PooledThread) it.next();
|
||||
if (thread.getLastUsedTime() < usedAfter) {
|
||||
stopList.add(thread);
|
||||
numToStop--;
|
||||
}
|
||||
}
|
||||
Iterator<PooledThread> stopIt = stopList.iterator();
|
||||
while (stopIt.hasNext()) {
|
||||
PooledThread thread = (PooledThread) stopIt.next();
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
int numToAdd = minSize - size();
|
||||
if (numToAdd > 0) {
|
||||
// should add some more to the pool
|
||||
for (int i = 0; i < numToAdd; i++) {
|
||||
growPool(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interrupt a named thread that is currently busy.
|
||||
* <p>
|
||||
* Returns the thread that was interrupted or null if the thread
|
||||
* was not found. If the thread was interrupted then it will
|
||||
* automatically be stopped and removed from the pool.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note that it may take some time to actually interrupt the thread so
|
||||
* an immediate test to see if the thread stopped will probably be wrong.
|
||||
* <pre><code>
|
||||
* ThreadPool test = ThreadPoolManager.getThreadPool("test");
|
||||
* PooledThread pt = test.interrupt("test.1");
|
||||
* if (pt == null) {
|
||||
* // the thread was not found, perhaps finished?
|
||||
* } else {
|
||||
* // give interrupt a little time to execute
|
||||
* Thread.sleep(1000);
|
||||
* boolean hasStopped = pt.isStopped();
|
||||
* //..
|
||||
* }
|
||||
* </code></pre>
|
||||
* </p>
|
||||
* @return the thread that was interrupted
|
||||
*/
|
||||
public PooledThread interrupt(String threadName) {
|
||||
PooledThread thread = getBusyThread(threadName);
|
||||
if (thread != null) {
|
||||
thread.interrupt();
|
||||
return thread;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a thread using its name from the busy list. Returns null if the
|
||||
* thread is not found in the busy list.
|
||||
*/
|
||||
public PooledThread getBusyThread(String threadName) {
|
||||
synchronized (freeList) {
|
||||
Iterator<PooledThread> it = getBusyThreads();
|
||||
while (it.hasNext()) {
|
||||
PooledThread pt = (PooledThread) it.next();
|
||||
if (pt.getName().equals(threadName)) {
|
||||
return pt;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Grow the pool with the option of either putting it on the available list,
|
||||
* or returning it.
|
||||
*/
|
||||
private PooledThread growPool(boolean andReturn) {
|
||||
|
||||
synchronized (freeList) {
|
||||
|
||||
String threadName = poolName + "." + uniqueThreadID++;
|
||||
PooledThread bgw = new PooledThread(this, threadName, isDaemon, threadPriority);
|
||||
bgw.start();
|
||||
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine("ThreadPool grow created [" + threadName + "] size[" + size() + "]");
|
||||
}
|
||||
if (andReturn) {
|
||||
return bgw;
|
||||
} else {
|
||||
freeList.add(bgw);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum amount of time in millis that Threads can be idle
|
||||
* before they are trimmed.
|
||||
*/
|
||||
public long getMaxIdleTime() {
|
||||
return maxIdleTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maxiumium amount of time in millis that Threads can be idle
|
||||
* before they are trimed.
|
||||
*/
|
||||
public void setMaxIdleTime(long maxIdleTime) {
|
||||
this.maxIdleTime = maxIdleTime;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,194 +1,177 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.server.lib.BackgroundThread;
|
||||
|
||||
/**
|
||||
* Singleton that manages a list of named ThreadPools.
|
||||
*/
|
||||
public class ThreadPoolManager implements Runnable {
|
||||
|
||||
private static final class Single {
|
||||
private static final ThreadPoolManager me = new ThreadPoolManager();
|
||||
}
|
||||
|
||||
private static int debugLevel = 0;
|
||||
|
||||
/**
|
||||
* set when the pools are being shutdown.
|
||||
*/
|
||||
private boolean isShuttingDown = false;
|
||||
|
||||
/**
|
||||
* Holds all the thread pools.
|
||||
*/
|
||||
private ConcurrentHashMap<String,ThreadPool> threadPoolCache = new ConcurrentHashMap<String, ThreadPool>();
|
||||
|
||||
/**
|
||||
* The default time threads are idle before they are stopped and removed.
|
||||
* This can occur when the pool grows larger than the min size and then goes idle
|
||||
* for some time.
|
||||
*/
|
||||
private long defaultIdleTime;
|
||||
|
||||
private ThreadPoolManager() {
|
||||
initialise();
|
||||
}
|
||||
|
||||
private void initialise() {
|
||||
|
||||
debugLevel = GlobalProperties.getInt("threadpool.debugLevel", 0);
|
||||
|
||||
defaultIdleTime = 1000 * GlobalProperties.getInt("threadpool.idletime", 60);
|
||||
|
||||
int freqIsSecs = GlobalProperties.getInt("threadpool.sleeptime", 30);
|
||||
|
||||
BackgroundThread.add(freqIsSecs, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the debug level.
|
||||
*/
|
||||
public static void setDebugLevel(int level) {
|
||||
debugLevel = level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the debug level.
|
||||
*/
|
||||
public static int getDebugLevel() {
|
||||
return debugLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodically maintains the pool size. Stops threads that have
|
||||
* been idle for too long and ensures the minimum number of threads.
|
||||
* <p>
|
||||
* To change this you can set the threadpool.idletime property:<br>
|
||||
* <br>
|
||||
* <b><code>## set threadpool idletime to 120 seconds</code></b><br>
|
||||
* <b><code>threadpool.idletime=120</code></b><br>
|
||||
* </p>
|
||||
*/
|
||||
public void run() {
|
||||
if (!isShuttingDown) {
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the named thread pool.
|
||||
*/
|
||||
public static ThreadPool getThreadPool(String poolName) {
|
||||
return Single.me.getPool(poolName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the named ThreadPool. If the ThreadPool doesn't exist it will be
|
||||
* created.
|
||||
*/
|
||||
private ThreadPool getPool(String poolName) {
|
||||
synchronized (this) {
|
||||
ThreadPool threadPool = (ThreadPool) threadPoolCache.get(poolName);
|
||||
if (threadPool == null) {
|
||||
threadPool = createThreadPool(poolName);
|
||||
threadPoolCache.put(poolName, threadPool);
|
||||
}
|
||||
return threadPool;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator of ThreadPools.
|
||||
* <p>
|
||||
* Note that the ThreadPools should not be removed by the iterator.
|
||||
* </p>
|
||||
*/
|
||||
public static Iterator<ThreadPool> pools() {
|
||||
return Single.me.threadPoolCache.values().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Maintain the size of all the thread pools.
|
||||
* Trims down to minimum size threads that have been idle for a while.
|
||||
* Adds threads if it is short of the minimum size.
|
||||
*/
|
||||
private void maintainPoolSize() {
|
||||
if (isShuttingDown){
|
||||
return;
|
||||
}
|
||||
synchronized (this) {
|
||||
|
||||
Iterator<ThreadPool> e = pools();
|
||||
while (e.hasNext()) {
|
||||
ThreadPool pool = (ThreadPool) e.next();
|
||||
pool.maintainPoolSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown all the ThreadPools nicely.
|
||||
* This will wait for all currently runnable and queued work to finish.
|
||||
*/
|
||||
public static void shutdown() {
|
||||
Single.me.shutdownPools();
|
||||
}
|
||||
|
||||
private void shutdownPools() {
|
||||
synchronized (this) {
|
||||
isShuttingDown = true;
|
||||
Iterator<ThreadPool> i = pools();
|
||||
while (i.hasNext()) {
|
||||
ThreadPool pool = (ThreadPool) i.next();
|
||||
pool.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ThreadPool createThreadPool(String poolName) {
|
||||
|
||||
int min = GlobalProperties.getInt("threadpool." + poolName + ".min", 0);
|
||||
int max = GlobalProperties.getInt("threadpool." + poolName + ".max", 100);
|
||||
|
||||
long idle = 1000 * GlobalProperties.getInt("threadpool." + poolName + ".idletime", -1);
|
||||
if (idle < 0) {
|
||||
idle = defaultIdleTime;
|
||||
}
|
||||
|
||||
boolean isDaemon = true;
|
||||
Integer priority = null;
|
||||
String threadPriority = GlobalProperties.get("threadpool." + poolName + ".priority", null);
|
||||
if (threadPriority != null) {
|
||||
priority = new Integer(threadPriority);
|
||||
}
|
||||
|
||||
ThreadPool newPool = new ThreadPool(poolName, isDaemon, priority);
|
||||
newPool.setMaxSize(max);
|
||||
newPool.setMinSize(min);
|
||||
newPool.setMaxIdleTime(idle);
|
||||
|
||||
return newPool;
|
||||
}
|
||||
|
||||
};
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.server.lib.BackgroundThread;
|
||||
|
||||
/**
|
||||
* Singleton that manages a list of named ThreadPools.
|
||||
*/
|
||||
public class ThreadPoolManager implements Runnable {
|
||||
|
||||
private static final class Single {
|
||||
private static final ThreadPoolManager me = new ThreadPoolManager();
|
||||
}
|
||||
|
||||
private static int debugLevel = 0;
|
||||
|
||||
/**
|
||||
* set when the pools are being shutdown.
|
||||
*/
|
||||
private boolean isShuttingDown = false;
|
||||
|
||||
/**
|
||||
* Holds all the thread pools.
|
||||
*/
|
||||
private ConcurrentHashMap<String,ThreadPool> threadPoolCache = new ConcurrentHashMap<String, ThreadPool>();
|
||||
|
||||
/**
|
||||
* The default time threads are idle before they are stopped and removed.
|
||||
* This can occur when the pool grows larger than the min size and then goes idle
|
||||
* for some time.
|
||||
*/
|
||||
private long defaultIdleTime;
|
||||
|
||||
private ThreadPoolManager() {
|
||||
initialise();
|
||||
}
|
||||
|
||||
private void initialise() {
|
||||
|
||||
debugLevel = GlobalProperties.getInt("threadpool.debugLevel", 0);
|
||||
|
||||
defaultIdleTime = 1000 * GlobalProperties.getInt("threadpool.idletime", 60);
|
||||
|
||||
int freqIsSecs = GlobalProperties.getInt("threadpool.sleeptime", 30);
|
||||
|
||||
BackgroundThread.add(freqIsSecs, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the debug level.
|
||||
*/
|
||||
public static void setDebugLevel(int level) {
|
||||
debugLevel = level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the debug level.
|
||||
*/
|
||||
public static int getDebugLevel() {
|
||||
return debugLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Periodically maintains the pool size. Stops threads that have
|
||||
* been idle for too long and ensures the minimum number of threads.
|
||||
* <p>
|
||||
* To change this you can set the threadpool.idletime property:<br>
|
||||
* <br>
|
||||
* <b><code>## set threadpool idletime to 120 seconds</code></b><br>
|
||||
* <b><code>threadpool.idletime=120</code></b><br>
|
||||
* </p>
|
||||
*/
|
||||
public void run() {
|
||||
if (!isShuttingDown) {
|
||||
maintainPoolSize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the named thread pool.
|
||||
*/
|
||||
public static ThreadPool getThreadPool(String poolName) {
|
||||
return Single.me.getPool(poolName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the named ThreadPool. If the ThreadPool doesn't exist it will be
|
||||
* created.
|
||||
*/
|
||||
private ThreadPool getPool(String poolName) {
|
||||
synchronized (this) {
|
||||
ThreadPool threadPool = (ThreadPool) threadPoolCache.get(poolName);
|
||||
if (threadPool == null) {
|
||||
threadPool = createThreadPool(poolName);
|
||||
threadPoolCache.put(poolName, threadPool);
|
||||
}
|
||||
return threadPool;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator of ThreadPools.
|
||||
* <p>
|
||||
* Note that the ThreadPools should not be removed by the iterator.
|
||||
* </p>
|
||||
*/
|
||||
public static Iterator<ThreadPool> pools() {
|
||||
return Single.me.threadPoolCache.values().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Maintain the size of all the thread pools.
|
||||
* Trims down to minimum size threads that have been idle for a while.
|
||||
* Adds threads if it is short of the minimum size.
|
||||
*/
|
||||
private void maintainPoolSize() {
|
||||
if (isShuttingDown){
|
||||
return;
|
||||
}
|
||||
synchronized (this) {
|
||||
|
||||
Iterator<ThreadPool> e = pools();
|
||||
while (e.hasNext()) {
|
||||
ThreadPool pool = (ThreadPool) e.next();
|
||||
pool.maintainPoolSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown all the ThreadPools nicely.
|
||||
* This will wait for all currently runnable and queued work to finish.
|
||||
*/
|
||||
public static void shutdown() {
|
||||
Single.me.shutdownPools();
|
||||
}
|
||||
|
||||
private void shutdownPools() {
|
||||
synchronized (this) {
|
||||
isShuttingDown = true;
|
||||
Iterator<ThreadPool> i = pools();
|
||||
while (i.hasNext()) {
|
||||
ThreadPool pool = (ThreadPool) i.next();
|
||||
pool.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ThreadPool createThreadPool(String poolName) {
|
||||
|
||||
int min = GlobalProperties.getInt("threadpool." + poolName + ".min", 0);
|
||||
int max = GlobalProperties.getInt("threadpool." + poolName + ".max", 100);
|
||||
|
||||
long idle = 1000 * GlobalProperties.getInt("threadpool." + poolName + ".idletime", -1);
|
||||
if (idle < 0) {
|
||||
idle = defaultIdleTime;
|
||||
}
|
||||
|
||||
boolean isDaemon = true;
|
||||
Integer priority = null;
|
||||
String threadPriority = GlobalProperties.get("threadpool." + poolName + ".priority", null);
|
||||
if (threadPriority != null) {
|
||||
priority = new Integer(threadPriority);
|
||||
}
|
||||
|
||||
ThreadPool newPool = new ThreadPool(poolName, isDaemon, priority);
|
||||
newPool.setMaxSize(max);
|
||||
newPool.setMinSize(min);
|
||||
newPool.setMaxIdleTime(idle);
|
||||
|
||||
return newPool;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -1,113 +1,96 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
|
||||
/**
|
||||
* Used internally by the ThreadPool to wrap a Runnable that is
|
||||
* going to be run.
|
||||
*
|
||||
* <p>Used to maintains some useful times about the Runnable in terms of when
|
||||
* it was queued and then eventually run.</p>
|
||||
*/
|
||||
public class Work {
|
||||
|
||||
/**
|
||||
* Create a Runnable Work.
|
||||
*/
|
||||
public Work(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated Runnable object.
|
||||
*/
|
||||
public Runnable getRunnable() {
|
||||
return runnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this work actually started.
|
||||
*/
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time this work actually started.
|
||||
*/
|
||||
public void setStartTime(long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this entered the queue.
|
||||
*/
|
||||
public long getEnterQueueTime() {
|
||||
return enterQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time this entered the queue.
|
||||
*/
|
||||
public void setEnterQueueTime(long enterQueueTime) {
|
||||
this.enterQueueTime = enterQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this left the queue.
|
||||
*/
|
||||
public long getExitQueueTime() {
|
||||
return exitQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time this work left the queue.
|
||||
*/
|
||||
public void setExitQueueTime(long exitQueueTime) {
|
||||
this.exitQueueTime = exitQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* The same as getDescription().
|
||||
*/
|
||||
public String toString() {
|
||||
return getDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a description of this work.
|
||||
*/
|
||||
public String getDescription() {
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("Work[");
|
||||
if (runnable != null){
|
||||
sb.append(runnable.toString());
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Runnable runnable;
|
||||
|
||||
private long exitQueueTime;
|
||||
private long enterQueueTime;
|
||||
private long startTime;
|
||||
|
||||
};
|
||||
package com.avaje.ebeaninternal.server.lib.thread;
|
||||
|
||||
|
||||
/**
|
||||
* Used internally by the ThreadPool to wrap a Runnable that is
|
||||
* going to be run.
|
||||
*
|
||||
* <p>Used to maintains some useful times about the Runnable in terms of when
|
||||
* it was queued and then eventually run.</p>
|
||||
*/
|
||||
public class Work {
|
||||
|
||||
/**
|
||||
* Create a Runnable Work.
|
||||
*/
|
||||
public Work(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the associated Runnable object.
|
||||
*/
|
||||
public Runnable getRunnable() {
|
||||
return runnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this work actually started.
|
||||
*/
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time this work actually started.
|
||||
*/
|
||||
public void setStartTime(long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this entered the queue.
|
||||
*/
|
||||
public long getEnterQueueTime() {
|
||||
return enterQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time this entered the queue.
|
||||
*/
|
||||
public void setEnterQueueTime(long enterQueueTime) {
|
||||
this.enterQueueTime = enterQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the time this left the queue.
|
||||
*/
|
||||
public long getExitQueueTime() {
|
||||
return exitQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time this work left the queue.
|
||||
*/
|
||||
public void setExitQueueTime(long exitQueueTime) {
|
||||
this.exitQueueTime = exitQueueTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* The same as getDescription().
|
||||
*/
|
||||
public String toString() {
|
||||
return getDescription();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a description of this work.
|
||||
*/
|
||||
public String getDescription() {
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("Work[");
|
||||
if (runnable != null){
|
||||
sb.append(runnable.toString());
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Runnable runnable;
|
||||
|
||||
private long exitQueueTime;
|
||||
private long enterQueueTime;
|
||||
private long startTime;
|
||||
|
||||
};
|
||||
Reference in new issue
Block a user