Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: New Recommendation

...

Code Block
bgColor#FFcccc
class NetworkHandler implements Runnable {
  private static ThreadGroup tg = new ThreadGroup("Chief");
  public void run() {

    try {
          method1();
          method2();
          Thread.sleep(500);		
    } catch(InterruptedException e) {}
  }

  public static void method1() throws InterruptedException {
    Thread t1 = new Thread(tg, new HandleRequest(), "t1");
    Thread t2 = new Thread(tg, new HandleRequest(), "t2");
    t1.start(); 
    t2.start(); 	
  }

  public static void method2() {			  
    Thread t3 = new Thread(tg, new HandleRequest(), "t3");
    Thread t4 = new Thread(tg, new HandleRequest(), "t4");		
    t3.start();
    t4.start();
  }
	
  public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread(tg, new NetworkHandler(), "t");
    t.start();
    System.out.println("Active Threads in Thread Group at point (1):" + t.getThreadGroup().getName() + " " + Thread.activeCount());
    Thread ta[] = new Thread[Thread.activeCount()];
		
    for(int i=0;i<500000;i++) {} // delay to demonstrate TOCTOU condition
		
    System.out.println("Active Threads in Thread Group at point (2):" + t.getThreadGroup().getName() + " " + Thread.activeCount());
		
    int n = Thread.enumerate(ta);
    System.out.println("Enumerating...");
    for(int i=0;i< n;i++) {
      System.out.println("Thread " + i + " = " + ta[i].getName());
    } 
  }
} 

class HandleRequest implements Runnable {
  public void run() {
  System.out.println("Active Threads in Thread Group (Handler thread invoked this): " + 
  Thread.currentThread().getThreadGroup().getName() + " " + Thread.activeCount());
		
  }
}

...