com.funambol.util
Class ThreadPool

java.lang.Object
  extended by com.funambol.util.ThreadPool

public class ThreadPool
extends java.lang.Object

ThreadPool manages threads which are created within the pool. The pool provides two main features: 1) threads created by the pool can be monitored for runtime exceptions. If an exception is not handled by the client code then the exception raises and reaches the ThreadPool which will handle it through a ThreadPoolMonitor. The client can provide its own monitor and handle the exception, or a default monitor will be used (@see ThreadPoolMonitor). The default monitor simply logs the exception. 2) checks if the number of concurrent threads (active) exceeds the threshold which is set at ThreadPool construction time. The ThreadPool does not enforce a restriction on the number of active threads. If the JVM cannot start a new thread the behavior is undefined. The ThreadPool logs the cases when the number of threads exceed the threshold. Then it tries to start the thread anyway.


Constructor Summary
ThreadPool(int numberOfThreads)
          Constructs a ThreadPool with a default monitor (@see ThreadPoolMonitor) and the given maximun number of concurrent threads
ThreadPool(ThreadPoolMonitor monitor, int numberOfThreads)
          Constructs a ThreadPool with the given monitor and the given maximum number of concurrent threads
 
Method Summary
 int getRunnableCount()
          Returns the number of threads belonging to this pool that are currently running (still active)
 java.lang.Thread startThread(java.lang.Runnable task)
          Start a new task in a separate thread which is monitored by the object monitor set in the constructor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(ThreadPoolMonitor monitor,
                  int numberOfThreads)
Constructs a ThreadPool with the given monitor and the given maximum number of concurrent threads

Parameters:
monitor - invoked in case of runtime exception
numberOfThreads - maximum number of threads

ThreadPool

public ThreadPool(int numberOfThreads)
Constructs a ThreadPool with a default monitor (@see ThreadPoolMonitor) and the given maximun number of concurrent threads

Parameters:
numberOfThreads - maximum number of threads
Method Detail

getRunnableCount

public int getRunnableCount()
Returns the number of threads belonging to this pool that are currently running (still active)


startThread

public java.lang.Thread startThread(java.lang.Runnable task)
Start a new task in a separate thread which is monitored by the object monitor set in the constructor. If there are no more threads available we just log an error and try to create a new thread anyway. We could easily implement a waiting strategy to wait for at least one running thread to finish, but this is not safe as we may end up in some deadlocks. As of today we mainly check that we never exceed the maximum number of councurrent threads, this is why we simply log if we exceed this value and then try to create the new thread anyway.

Parameters:
task - is the Runnable object to be executed in a new thread
Returns:
the new thread that has been spawned


Copyright © 2001-2009 Funambol.