«^»
11.3. Using the class ClockStdout in the UseClockStdout program

The main method of the UseClockStdout program (given below) creates an object (tClockStdout) of the class ClockStdout, and then calls tClockStdout's start method. However, the class ClockStdout does not itself declare a start method, and so it is java.lang.Thread's start method that gets called. As explained earlier, this does two things:

So we now have two threads of activity that are running concurrently: the main method and the tClockStdout.run method. Having started the tClockStdout thread, the main method then goes on to output the digits from 0 to 7 stopping for one second after it has output each digit:
0805: public class UseClockStdout {                         // UseClockStdout.java
0806:    public static void main(String[ ] args) {
0807:       System.out.println("UseClockStdout program");
0808:       ClockStdout tClockStdout = new ClockStdout();
0809:       tClockStdout.start();
0810:       for ( int count = 0; count < 8 ; count++ ) {
0811:          System.out.println("count is: " + count);
0812:          try { Thread.sleep(1000); }
0813:          catch ( InterruptedException tInterruptedException ) { }
0814:       }
0815:       System.out.println("UseClockStdout program");
0816:    }
0817: }
Here is the sort of output that the program produces:
UseClockStdout program
count is: 0
Sat Jun 14 15:49:15 GMT+01:00 1997
count is: 1
count is: 2
Sat Jun 14 15:49:17 GMT+01:00 1997
count is: 3
count is: 4
Sat Jun 14 15:49:19 GMT+01:00 1997
count is: 5
count is: 6
Sat Jun 14 15:49:21 GMT+01:00 1997
count is: 7
UseClockStdout program
Sat Jun 14 15:49:23 GMT+01:00 1997
Sat Jun 14 15:49:25 GMT+01:00 1997
Sat Jun 14 15:49:27 GMT+01:00 1997
Sat Jun 14 15:49:29 GMT+01:00 1997
...
You can see that the output is from both threads, and that the program will not finish because the tClockStdout thread is an infinite loop. So, if you execute this program, you will need to press Ctrl/C to stop its execution.

Two ways of getting the program to terminate properly are: