Here is the code of the 4 classes
the controller manage a thread pool, the reportaction contain params for the work to do by thread, reportviewer do the work ans the fifo classe is used to manage actions as fifo
public class ThreadPoolController implements Runnable{
/**
* controller instance
*/
public static ThreadPoolController instance;
/***
* number of threads in the pool
*/
public static int nbThreads = 5;
/**
* workers
*/
private ReportRetriever[] workers;
/**
* states associated with threads (true idf the thread is free, false if it is running)
*/
private boolean[] states;
/**
* object of synchronization
*/
private static Object synchronizedObjet = new Object();
public FIFOActionQueue fifoinstance = null;
/**
* constructor
*/
private ThreadPoolController(int nbTh) {
super();
fifoinstance = FIFOActionQueue.getInstance();
workers = new ReportRetriever[nbTh];
states = new boolean[nbTh];
for( int i=0; i<workers.length; i++){
workers[i] = new ReportRetriever(i);
states[i] = true; //free thread
}
------------------------------------------------------------------------
/**
* FIFOActionQueue.java, Created on 10 mars 2006 at 14:57:47
*
*/
import java.util.ArrayList;
/**
* @author tarik.el-berrak
*
*/
public class FIFOActionQueue{
/**
* contains action elements
*/
public ArrayList contents;
/**
* queue instance
*/
public static FIFOActionQueue instance;
/**
* object of synchronization
*/
private static Object synchronizedObjet = new Object();
/**
* the constructor
*/
private FIFOActionQueue() {
contents = new ArrayList();
}
/**
*
* @return an instance of a queue, sychronized for multithreahding
*/
public static FIFOActionQueue getInstance() {
if (null == instance) {
synchronized(synchronizedObjet){
instance = new FIFOActionQueue();
}
}
return instance;
}
/**
* add a report action to a collection
*/
public synchronized void add(ReportAction a){
contents.add(a);
}
/**
* true if the queue is empty
*/
public boolean isEmpty() {
return contents.isEmpty();
}
public synchronized void remove(ReportAction a) {
contents.remove(a);
}
public int size() {
return contents.size();
}
public synchronized void trim(){
this.contents.trimToSize();
}
/**
* get the head of the collection
* @return
*/
public synchronized ReportAction poll(){
if( this.contents.isEmpty()) return null;
ReportAction ra = (ReportAction)this.contents.get(0);
if( ra != null ) {
this.contents.remove(ra);
this.trim();
}
return ra;
}
}
---------------------------------------------------------------------------
/**
* ReportAction.java, Created on 10 mars 2006 at 14:11:45
*
*/
/**
* @author tarik.el-berrak
*
*/
public class ReportAction {
private int idReport;
private int idDeal;
private
String url;
private String jndi;
private String jndiFactory;
private String archivePath;
/**
* the constructor
*/
public ReportAction(int _idr, int _idd, String _url, String _jndi, String _jndiFactory, String _archivePath) {
super();
this.idReport=_idr;
this.idDeal=_idd;
this.url=_url;
this.jndi=_jndi;
this.jndiFactory=_jndiFactory;
this.archivePath=_archivePath;
}
public boolean equals(Object arg0) {
return (((ReportAction)arg0).idDeal == this.idDeal && ((ReportAction)arg0).idReport == this.idReport);
}
public int getIdDeal() {
return idDeal;
}
public void setIdDeal(int idDeal) {
this.idDeal = idDeal;
}
public int getIdReport() {
return idReport;
}
public void setIdReport(int idReport) {
this.idReport = idReport;
}
public String getJndi() {
return jndi;
}
public void setJndi(String jndi) {
this.jndi = jndi;
}
public String getJndiFactory() {
return jndiFactory;
}
public void setJndiFactory(String jndiFactory) {
this.jndiFactory = jndiFactory;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getArchivePath() {
return archivePath;
}
public void setArchivePath(String archivePath) {
this.archivePath = archivePath;
}
}
----------------------------------------------------------------------------
/**
* ReportRetriever.java, Created on 13 mars 2006 at 11:15:04
*
*/
/**
* @author tarik.el-berrak
*
*/
public class ReportRetriever extends Thread {
private int workerID;
private ReportAction workToDo;
private boolean state;
/**
* the constructor
*/
public ReportRetriever(int id) {
this.workerID = id;
}
/**
* perform the work
* @param ra Report Action
* @return
*/
public boolean runWork(ReportAction ra){
System.out.println(ra.toString());
return true;
}
public void run(){
runWork(getWorkToDo());
}
public ReportAction getWorkToDo() {
return workToDo;
}
public void setWorkToDo(ReportAction workToDo) {
this.workToDo = workToDo;
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public int getWorkerID() {
return workerID;
}
public void setWorkerID(int workerID) {
this.workerID = workerID;
}
public static void main(String args[]){
ReportRetriever rr = new ReportRetriever(1);
ReportAction ra = new ReportAction(100,20,"","","","");
rr.setWorkToDo(ra);
}
}