import java.util.concurrent.*;

public class ThreadPoolDemo
{
  public static void main(String[] args) 
  {
    int numTasks = Integer.parseInt(args[0]);
    ExecutorService pool =                  // create an unlimited pool
      java.util.concurrent.Executors.newCachedThreadPool();

    for (int i=0; i<numTasks; i++)          // create a pool
      pool.execute(new Task(i));
		
    // sleep for 5 seconds
    try { Thread.sleep(5000); } catch (InterruptedException ie) { }

    pool.shutdown();                        // destroy the pool
  }
}

class Task implements Runnable
{
  private int id;
  public Task(int id)                      // thread constructor
  {
    this.id = id;
  } 
  public void run() 
  {
    for (int i=0; i<100; i++)
      System.out.println(id + ": I am working on a task.");
  }
}
