package visibility;

/**
 * Starts a spinner, asks it to shut down after a second, then waits up to three
 * seconds for it to finish.
 */
public class VisibilityDemo {

	public static void main ( String[] args ) throws InterruptedException {
		// set accordingly to use the volatile spinner or the plain one
		boolean useVolatile = true;

		Spinner plain = new Spinner();
		VolatileSpinner vol = new VolatileSpinner();
		Thread spinner;
		if ( useVolatile ) {
			spinner = new Thread(vol,"spinner");
		} else {
			spinner = new Thread(plain,"spinner");
		}
		spinner.setDaemon(true); // so the program can still exit if the spinner
		                         // never shuts down
		spinner.start();

		Thread.sleep(1000);
		System.out.println("main: calling shutdown()");
		if ( useVolatile ) {
			vol.shutdown();
		} else {
			plain.shutdown();
		}

		spinner.join(3000); // wait at most 3 seconds
		if ( spinner.isAlive() ) {
			System.out.println("main: 3 seconds later, the spinner is still running");
		} else {
			System.out.println("main: the spinner has finished");
		}
	}

}
