Java Threads 1

// this is file ThreadTester.java
// complile with "javac ThreadTester.java"
// after both classes have been compliled, then execute this one
// with "java ThreadTester"
// use CTRL-C to terminate

public class ThreadTester {
public static void main(String argv[]) {
TestThread aRP = new TestThread();
System.out.println("ThreadTester");
new Thread(aRP, "Thread 1 ").start();
new Thread(aRP, "Thread 2 ").start();
new Thread(aRP, "Thread 3 ").start();
new Thread(aRP, "Thread 4 ").start();
}
}


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

public class TestThread implements Runnable {
private int count;
public void run() {
while (true) {
count++;
System.out.println(Thread.currentThread().getName()
+ count);
}
}
}

You may wish to experiment with the sleep function to slow down the threads. Sleep measured in milliseconds.

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