Java Thread Test20 arrays of threads


// Ttest21.java
// it contains a test java program, that defines a class Ttest21 with
// a main function and creates threads from the class Thread21.java
// demonstrates inter-thread communication (message passing)

// compile this file with the command "javac Ttest21.java"
// your path must include the bin directory of the jdk

// run the program with "java Ttest21"
// on some systems


public class Ttest21 {
	          public static Thread21 tptr[] = new Thread21[4];
        public static void main(String argv[]) {
		  int i;

                  System.out.println("Ttest21a");

		  for (i=0;i<2;i++) {
		      tptr[i] = new Thread21();
		  }

                  System.out.println("Ttest21b");
		  for (i=0;i<2;i++) {
		      tptr[i].start();
		  }

                  System.out.println("Ttest21c");
		  tptr[0].sayHello();
        }
	public static void passcall(int id) {
		  tptr[id].sayHello();
	}
}

//************* second file ******************************************

// this is file Thread21.java
// compile with "javac Thread21.java"

//demonstrates interthread communcation

class Thread21 extends Thread {
        private int count = 0;
	public Thread21() {
	   System.out.println("Hi from thread " + getName());
	}
	
        public void run() {
              count++;
              System.out.println(getName() + "  " + count);
	      try {
	          Thread.sleep(500);
	      } catch (InterruptedException e) {
        	  return;
	      }
	      Ttest21.passcall(1);
        }
	public void sayHello() {
              System.out.println("Hello from " + getName() + "  " + count);
	}

}