/**
 * An application that illustrates some of the problems that can arise
 * when using shared mutable state, and how synchronization can
 * prevent them.
 *
 * If the application is run with no command line arguments it will
 * use a NonatomicInteger. If any command line arguments are provided
 * it will use a SynchronizedInteger.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CounterDriver
{
    private static final int ITERATIONS = 1000000;    
    private static final int THREADS    = 5;    

    /**
     * The entry point of the application.
     *
     * @param args  The command line arguments
     */
    public static void main(String[] args)
    {
        MutableInteger       mutable;

        if ((args != null) && (args.length > 0))
            mutable = new SynchronizedInteger(0);
        else
            mutable = new NonatomicInteger(0);
        
        for (int i=0; i<THREADS; i++)
        {
            Thread thread = new Thread(new Counter(mutable, ITERATIONS));
            thread.start();
        }
    }
    
}
