// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class TestThreadPool {
    public static void main(String[] args) throws Exception {
        ThreadPool tp = new ThreadPool(1000);
        int[] a = new int[100000];
        for (int i = 0; i < a.length; i++) {
            tp.addTask(new Test(a, i));
        }
        while (tp.queueSize() > 0) {
            System.out.println(tp.queueSize());
            Thread.sleep(5000);
        }
        tp.stop();
        for (int i = 0; i < a.length; i++) {
            if (a[i] != i) {
                System.out.println("Error in task " + i);
            }
        }

    }
}

class Test implements Runnable {
    private int[] a;
    private int ix;
    public Test(int[] a, int ix) {
        this.a = a;
        this.ix = ix;
    }
    public void run() {
        try {
            Thread.sleep((int) (1000 * Math.random()));
        } catch (InterruptedException e) {
        }
        a[ix] = ix;
    }
}
