FileDocCategorySizeDatePackage
Clock.javaAPI DocExample1411Wed Apr 20 15:11:48 BST 2005None

Clock

public class Clock extends Thread

Fields Summary
volatile boolean
keepRunning
Constructors Summary
public Clock()

    
           // The constructor
        setDaemon(true); // Daemon thread: interpreter can exit while it runs
    
Methods Summary
public static voidmain(java.lang.String[] args)

        Clock c = new Clock();                 // Create a Clock thread
        c.start();                             // Start it
        try {  SECONDS.sleep(10); }            // Wait 10 seconds
        catch(InterruptedException ignore) {}  // Ignore interrupts
        // Now stop the clock thread.  We could also use c.interrupt()
        c.pleaseStop();
    
public voidpleaseStop()

 keepRunning = false; 
public voidrun()

        // The body of the thread
        while(keepRunning) {   // This thread runs until asked to stop
            long now = System.currentTimeMillis();    // Get current time
            System.out.printf("%tr%n", now);          // Print it out
            try { Thread.sleep(1000); }               // Wait 1000 milliseconds
            catch (InterruptedException e) { return; }// Quit on interrupt
        }