com.rhi.architecture.threads
Class ThreadPoolImpl

java.lang.Object
  extended bycom.rhi.architecture.threads.ThreadPoolImpl
All Implemented Interfaces:
ThreadPool

public class ThreadPoolImpl
extends java.lang.Object
implements ThreadPool

Custom ThreadPool Implementation. The ThreadPool is started with predefined set of WorkerThreads which actually perform the work. This ThreadPool is suitable for scenarios work is split across mutiple threads and main thread waits for all the worker threads to finish and assigns work to all (or part) of worker threads. The ThreadPool can be asked to find a worked thread and assign work to it using execute method. Using join() method the caller will wait for all the worker threads to finish. IMPORTANT NOTE(S): 1. This ThreadPool is suitable for use by a single thread, not by mutliple threads/requests requesting ThreadPool to execute work. 2. It is the responsibility of the caller to make sure that it requests at maximum 'poolsize' amount of work to be performed by the ThreadPool. If no workers are free an RuntimeException is thrown.

Version:
1.0
Author:
Chandra Ilindra

Constructor Summary
ThreadPoolImpl(int size)
          Constructor - constructs a ThreadPool of given size.
 
Method Summary
 void close()
          Mark all threads to shutdown and notify them, just in case they are sleeping.
 void execute(java.lang.Runnable work)
          Executes a given 'work'.
 void join()
          Wait for all the worker threads to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPoolImpl

public ThreadPoolImpl(int size)
Constructor - constructs a ThreadPool of given size. Constructs returns after all the threads have been created and started.

Parameters:
size - - size of the pool
Since:
1.0
Method Detail

execute

public void execute(java.lang.Runnable work)
             throws java.lang.RuntimeException
Executes a given 'work'. Finds a free worker thread to execute the work - sets the work and notifies the worked thread to wake up and perform the work.

Specified by:
execute in interface ThreadPool
Parameters:
work - - work to be performed.
Throws:
java.lang.RuntimeException
Since:
1.0

join

public void join()
Wait for all the worker threads to finish. If there are workers still performing the work, sleep for a second and then check again.

Specified by:
join in interface ThreadPool
Since:
1.0

close

public void close()
Mark all threads to shutdown and notify them, just in case they are sleeping.

Specified by:
close in interface ThreadPool
Since:
1.0


Copyright © 2002-2004 Robert Half International. All Rights Reserved.