public Counter( String threadName )
{
super( threadName
);
System.out.println(
this );
start();
}
public void run()
{
try
{
while ( currentValue < 5 )
{
System.out.println( getName() + ": " + (currentValue++) );
Thread.sleep( 500 );
}
}
catch (
InterruptedException ignore )
{
}
System.out.println(
"Exit from " + getName() + "." );
}
public int getValue()
{
return currentValue;
}
}
public class AnotherClient
{
public static void main( String args[] )
{
Counter cA = new
Counter( "Counter A" );
Counter cB = new
Counter( "Counter B" );
try
{
System.out.println( "Wait for the child threads to finish." );
cA.join();
cB.join();
if ( !cA.isAlive() ) //
prove to reader that cA is not alive
{
System.out.println( "Counter A not alive." );
}
if ( !cB.isAlive() )
{
System.out.println("Counter B not alive.");
}
}
catch (
InterruptedException
e )
{
}
System.out.println(
"Exit from Main Thread." );
}
}