Monday, June 22, 2009

Create your own Thread Pool in Java

ThreadPool

Thread pool implementation in java.

Introduction

This is a very simple Thread pool that makes sure specified number of threads are running at any instance of time.You can use this as part of say Data base connection object pooling or to avoid thread fork bomb.

Details

Go code.google.com and search for threadpoolingjava project Navigate to downloads page. Download and unzip the archive which includes java source file and GNU V3 licence document. Use the source as part of your project . Has no dependencies on 3rd party libraries other than standard java libraries. The source is well commented which guides you with the method call sequences.

NOTE:

Please make sure you got a copy of GNU GPL licence V3 along with the source code.



import java.util.*;


public class ThreadPool implements Runnable {
public static int POOL_SIZE = 2;// Default pool size
public static int POOL_STATUS_CHECK_TIME = 1000;//In milli seconds
private List threadsInPool;
private Thread self;

public ThreadPool() {
....
}

public ThreadPool(int size) {
....
}

public void addThreadToPool(Runnable th) {
.......
}

public void kickStart() {
.....
}

public void run() {
.........
}


public boolean isPoolAlive() {
........
}

private void pausePool() {
........
}
}