import java.io.*; import java.util.*; public class ThreadTestMain { public static void main(String[] args) { boolean help_menu = args.length==1 && (args[0].equals("help") || args[0].equals("-h")); if (args.length==0 || help_menu) { menu(); if (help_menu) System.exit(1); } ThreadTest[] ta = new ThreadTest[100]; //runnables Thread[] T = new Thread[100]; //threads //start some jobs and threads to run them by //based on cmd line args as snooze times in seconds int num_threads = args.length; int orig_num_threads = num_threads; for (int i=0; i0) cmd = cmd_line.charAt(0); else cmd = 'h'; int thread_id, i, new_snooze; switch (cmd) { case 'k': thread_id = Integer.parseInt(cmd_line.substring(1).trim()); i = findThread(thread_id, ta, num_threads); if (i != -1) { ta[i].pleaseStop(); ta[i] = ta[num_threads-1]; //unordered list //deprecated: also, pleaseStop not in Thread error //ta[thread_i].stop(); num_threads--; } else System.out.println("no such thread"); break; case 'p': thread_id = Integer.parseInt(cmd_line.substring(1).trim()); i = findThread(thread_id, ta, num_threads); if (i != -1) { ta[i].pleasePause(); } else System.out.println("no such thread"); break; case 'c': thread_id = Integer.parseInt(cmd_line.substring(1).trim()); i = findThread(thread_id, ta, num_threads); if (i != -1) { ta[i].pleaseContinue(); } else System.out.println("no such thread"); break; case 'u': StringTokenizer two_ints = new StringTokenizer(cmd_line.substring(1)); thread_id = Integer.parseInt(two_ints.nextToken()); try { new_snooze = Integer.parseInt(two_ints.nextToken()); } catch (NoSuchElementException e) { new_snooze = 1000; //default snooze } //debug: //System.out.println(thread_id+" "+new_snooze); i = findThread(thread_id, ta, num_threads); if (i != -1) { ta[i].updateSnooze(new_snooze); } else System.out.println("no such thread"); break; case 's': try { new_snooze =Integer.parseInt(cmd_line.substring(1).trim()); } catch (NumberFormatException e) { new_snooze = 1000; } ta[num_threads] = new ThreadTest(new_snooze); T[num_threads] = new Thread( ta[num_threads] ); T[num_threads].start(); num_threads++; break; case 'h': menu(); break; case 'q': break; default: System.out.println("unknown command"); } } while (cmd != 'q'); } //so don't need throws IOException (from readLine) for method catch (IOException e) {} //kill remaining threads int i = 0; while (num_threads > 0) { if (ta[i] != null) { ta[i].pleaseStop(); ta[i] = null; num_threads--; } i++; } System.out.println("Main is done"); } private static int findThread(int thread_id, ThreadTest[] ta, int num_threads) { int i=0; boolean found = false; while (i