Java Thread Test3
// Ttest3.java
// it contains a test java program, that defines a class jtest with
// a main function that prints howdy, and creates threads from the
// class Thread3.java
// It passes parameters to the threads, a name and an ID number.
// compile this file with the command "javac Ttest3.java"
// your path must include the bin directory of the jdk
// run the program with "java Ttest3"
// on some systems
class Ttest3 {
public static void main(String argv[]) {
System.out.println("Howdy from Ttest3");
new Thread3("Thread1", 0); //creates a thread
new Thread3("Thread2", 1); //creates another thread
}
}
//************* second file *************
// Thread3.java
// It is a thread class to be run from Ttest3
// It receives parameters from the parent, a thread name and ID number
// The thread runs forever (until Ctrl-C at keyboard).
// compile this file with the command "javac Thread3.java"
// your path must include the bin directory of the jdk
public class Thread3 extends Thread {
private int myid; // private to this thread
private int count; // private count of iterations
Thread3(String name, int me) { //constructor - executed when thread is created with new
super(name); //super is part of the Thread class
myid=me;
count = 0; //initialize count
start(); // by default calls method "run"
}
public void run() {
while (true) {
count++;
System.out.println("Howdy from " + Thread.currentThread().getName() +
", id=" + myid + " count is " + count);
try {
Thread.sleep(1000); //milliseconds
} catch (InterruptedException e) {
return;
}
}
}
}