Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Pool implementation

 
Sandeep Lakshmipathy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the following implementation of pooling correct. I see that when i run out of the threads in the pool and i am creating new threads to handle the requests, but it does not seem to happen. Something seems to be wrong there. Can any one help me out. Thanks in advance.
Bye
=================================
public class Main {

public static void main(String []a) throws Exception{
Work w1 = new Work();
Work2 w2 = new Work2();

ThreadPool tp = new ThreadPool(5);
for(int j=0; j<10; j++)
tp.execute(w1);//(new Thread(w1)).start();//
//Thread.sleep(5000);
Thread.sleep(50000); // allowing children to end
}
}
class Work implements Runnable {
static int count = 0;
public synchronized void run(){

try{
Thread.sleep(5000);
}catch(Exception e){ System.out.println("" + e);}

System.out.println("count : " + count++);
}
}
===================================

public class WorkerThread extends Thread{
Runnable runner = null;
private static boolean stopAll = false;

WorkerThread(){

}
public synchronized void run(){
while(!stopAll){
if(!(ThreadPool.taskVect).isEmpty()){
runner = (Runnable)ThreadPool.taskVect.remove(0); // removing from thread pool
System.out.println(" starting thread : " );
ThreadPool.poolVect.remove(this);
System.out.println(ThreadPool.poolVect );
//((Thread)runner).start();
runner.run();
ThreadPool.poolVect.add(this); // adding back
}
else
Thread.yield();
}
}

public static void stopAllThreads(){
stopAll = true;
//notifyAll();
}
}

============================

import java.util.*;
public class ThreadPool{

public static Vector taskVect = null;
public static Vector poolVect = null;
private final int MAX_THREADS = 100;
public static boolean req = false;
WorkerThread tpool[] = new WorkerThread[MAX_THREADS];

public ThreadPool(int maxthreads){
taskVect = new Vector(); //A Task pool.
poolVect = new Vector(); // A Thread pool.
/** makes sure that atleast one thread is in the pool */
if(maxthreads < 1) maxthreads = 1;

for(int i=0; i<maxthreads; i++){
tpool[i] = new WorkerThread();
tpool[i].setDaemon(true);
tpool[i].start();
poolVect.add(tpool[i]);
}

}
static int count;
static int count1;

public synchronized void execute(Runnable th){
System.out.println(poolVect.isEmpty());
if(!poolVect.isEmpty())
{
System.out.println("I" + count1++ + " " + taskVect);


taskVect.add(th);
notifyAll();
}
else{

System.out.println("Sorry " + ++count); // HANDLE CREATING NEW THREADS
//when pool runs out of threads
poolVect.add(new WorkerThread());
}


}// end execute()

}// end class ThreadPool
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code seems to have some logic error. Here is a simple implementation of thread pool. The code is not tested (nor compiled), but it should clear the basic idea.

public class ThreadPool {
private PoolableThread[] pool;
public ThreadPool(int size) {
pool = new PoolableThread[size];
for(int i=0; i < size; i++ ) {
pool[i] = new PoolableThread(this);
}
}
public synchronized Thread getThread(Runnable runner) throws IllegalStateException{
if(pool == null) {
throw IllegalStateException("pool has been stoped");
}
for(int i=0; i < pool.length; i++) {
if(pool[i].isWaiting()) {
pool[i].setRunner(runner);
return pool[i];
}
}
return new Thread(runner);
}
public synchronized void stop() {
for(int i=0; i < pool.length; i++) {
pool[i].stop();
}
pool = null;
}
//static nested class starts
static PoolableThread extends Thread {
private volatile boolean waiting = true;
private volatile boolean stop = false;
private volatile boolean started = false;
private final Object monitor = new Object();
private volatile Runnable runner;
public void run() {
while(!stop) {
waiting = false;
try {
runner.run();
}catch(Throwable t) { //catch everything
System.out.println("Runnable "+ runner + " caused " +t);
}
try {
synchronized(monitor) {
waiting = true;
monitor.wait();
}
}catch(Exception ex) {
//ignore
}
}//while ends
}//run ends
public void start() {
if(!started) {
startted = true;
super.start();
}
else {
try {
synchronized(monitor) {
monitor.notify();
}
}catch(Exception ex) {
//ignore
}//else ends
}//start ends
public void setRunner(Runnable runner) {
this.runner = runner;
}
public boolean isWaiting();
return waiting;
}

public void stopThread() {
stop = true;
}
}//nested class ends
}//pool ends
[ March 17, 2002: Message edited by: zakaria haque ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!