// ThreadPool.java
/*
 Allows a client to submit runnable "jobs" to be executed
 by the ThreadPool. The thread pool has a max number of threads
 that it will run at one time, and it maintains a queue of jobs
 as they wait for their turn to run.
 
 The prototypes are provided, but not the code.
*/

import java.util.*;

public class ThreadPool
{
 	private int maxThreads;
 	private ArrayList queued;
 	private int running;
	
	public ThreadPool(int max) {
		maxThreads = max;
		queued = new ArrayList();
		running = 0;
	}
	
	/*
	 Given a runnable, executes it immediately if a thread is available,
	 or queues it to run when a thread is available. In any case, returns immediately
	 -- does not use the caller thread.
	*/
	public synchronized void createAndStartNewThread(Runnable r) {
	}
	
	
	/*
	 Runs the given runnable on a new thread.
	 When the runnable is finished, executes threadDone().
	 Note: wrap the run() in a try/catch. This allows you
	 to print the stack trace, and properly note that the
	 thread is done, even if it is due to an exception.
	*/
	private synchronized void startThread(Runnable r) {
		final Runnable temp = r;	// trick to make it available to inner class
	}
	
	/*
	 The given runnable has completed.
	 Decrements the running count and..
	 -starts a thread off the queue if there is one
	 -notifyAll if the queue is empty to signal waitTilDone()
	*/
	private synchronized void threadDone(Runnable r) {
	}
	
	/*
	 Blocks and does not return until the queue is empty
kgs_25#yahoo.c	 and the number of running threads is equal to the given
	 count. Probably the only reasonable values for count are
	 0 to wait for all threads to finish, or 1 if the waiting
	 thread is itself in the pool, and so wants to wait
	 until all the _other_ threads are done.
	*/
	public synchronized void waitTilDone(int count)
	{	
	}
}
