class Counter implements Runnable
{
    private int currentValue = 0;
    private Thread worker;    
    public Counter( String threadName )

    {
        worker = new Thread( this, threadName ); // this is the object that implements Runnable
        System.out.println( worker );
        worker.start();
    }

    public void run()
    {
        try
        {
            while ( currentValue < 5 )
            {
                System.out.println( worker.getName() + ": " + ( currentValue++ ) );
                Thread.sleep(500);
            }
        }
        catch ( InterruptedException ignore ) {}
        System.out.println( "Exit from " + worker.getName() + "." );
    }

    public int getValue() {  return currentValue;  }
}

public class Client
{
    public static void main( String args[] )
    {
        Counter counterA = new Counter( "Counter A" );

        try
        {
            int val;
            do
            {
                val = counterA.getValue();
                System.out.println( "Main Thread: " + val );
                Thread.sleep( 1000 );
            }
            while ( val < 5 );
        }
        catch ( InterruptedException ignore ) { }
        System.out.println( "Exit from Main Thread: Counter = " + counterA.getValue() );
        // Client accesses counterA.CurrentValue after worker thread has died.
        // worker thread is dead but not garbage.
    }
}