/**
 * An implementation of Runnable that increases a MutableInteger.
 * It can be used to illustrate problems that can arise when using shared 
 * mutable state.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Counter implements Runnable
{
    private int                iterations;
    private MutableInteger     mutable;
    
    /**
     * Explicit Value Constructor.
     *
     * @param mutable    The MutableInteger to use
     * @param iterations The number of times to increment the MutableInteger
     */
    public Counter(MutableInteger mutable, int iterations)
    {
        this.mutable    = mutable;        
        this.iterations = iterations;        
    }
    
    /**
     * The code to run in the thread of execution.
     */
    public void run()
    {
        for (int i=0; i<iterations; i++)
        {
            mutable.incrementAndGet();
        }
        System.out.printf("Value: %d\n", mutable.get());
    }
}
