package edu.nyu.cs9053.concurrency.chapter14.stop;

/**
 * User: blangel
 * Date: 7/26/14
 * Time: 1:32 PM
 */
public class ThreadStop implements Runnable {

    private final int alwaysEqualsB = 0;

    private final int b = 0;

    @Override public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000); // do nothing
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                }
            }
        } catch (ThreadDeath td) {
            // allow finally block to run
        } finally {
            cleanup();
        }
    }

    private void cleanup() {
        System.out.printf("cleaning up %s [ %s | %d == %d ] %n", Thread.currentThread().getName(),
                (alwaysEqualsB == b), alwaysEqualsB, b);
    }
}
