package shutdown;

public class SlowCountdown2 implements Runnable {

	private int value_;
	private String indent_;

	private volatile boolean shutdown_;
	private volatile Thread thread_;

	public SlowCountdown2 ( int value, String indent ) {
		value_ = value;
		indent_ = indent;
		shutdown_ = false;
		thread_ = null;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run () {
		thread_ = Thread.currentThread();
		for ( ; value_ >= 0 && !shutdown_ ; value_-- ) {
			System.out.println(indent_ + value_);
			try {
				Thread.sleep(1000);
			} catch ( InterruptedException e ) {
				System.out.println(indent_ + "interrupted!");
				if ( shutdown_ ) { break; }
			}
		}
		System.out.println(indent_ + "done!");
	}

	public void shutdown () {
		shutdown_ = true;
		thread_.interrupt();
	}

}
