Android Open Source - coursera-android-pos Simple Atomic Long Test






From Project

Back to project page coursera-android-pos.

License

The source code is released under:

MIT License

If you think the Android project coursera-android-pos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Import the necessary Java synchronization and scheduling classes.
// w w  w .  j a  va  2  s  .c o m
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * @class SimpleAtomicLongTest
 *
 * @brief This class tests our implementation of SimpleAtomicLong to ensure
 *        it works properly.
 */
class SimpleAtomicLongTest
{
    /**
     * Number of iterations to run the test.
     */
    final static long mMaxIterations = 10000000;

    /**
     * Barrier synchronizer that controls when the two threads start
     * running the test.
     */
    final static CyclicBarrier mStartBarrier = new CyclicBarrier(2);

    /**
     * Barrier synchronizer that controls when the main thread can
     * return.
     */
    final static CountDownLatch mStopLatch = new CountDownLatch(2);

    /**
     * An instance of our implementation of SimpleAtomicLong.
     */
    final static SimpleAtomicLong mCounter = new SimpleAtomicLong(0);

    /**
     * @brief This class runs the test by invoking a command each time
     *        through the loop.
     */
    static class RunTest implements Runnable
    { 
        /**
         * A Command that determines what operation is done within the
         * loop.
         */
        private Runnable mCommand;

        /**
         * Store the command in a data member field.
         */
        RunTest(Runnable command) {
            mCommand = command;
        }
        
        /**
         * Run the command within a loop.
         */
        public void run() {
            try
                {
                    /**
                     * Wait for both Threads to start running before
                     * beginning the loop.
                     */
                    mStartBarrier.await();

                    for (int i = 0; i < mMaxIterations; ++i)
                        mCommand.run();

                    /**
                     * Inform the main thread that we're done.
                     */
                    mStopLatch.countDown();
                }
            catch (Exception e) {
                System.out.println("problem here");
            }
        }
    }

    /**
     * Main entry point method that runs the test.
     */
    public static void main(String[] args) {
        try {
            System.out.println("Starting SimpleAtomicLongTest");

            /**
             * A Runnable command that decrements the mCounter.
             */
            final Runnable decrementCommand =
                new Runnable() { public void run() { mCounter.getAndDecrement(); } };
            
            /**
             * A Runnable command that decrements the mCounter.
             */
            final Runnable incrementCommand =
                new Runnable() { public void run() { mCounter.getAndIncrement(); } };

            /**
             * Start a Thread whose Runnable command decrements the
             * SimpleAtomicLong mMaxIterations number of times.
             */
            new Thread(new RunTest(decrementCommand)).start();

            /**
             * Start a Thread whose Runnable command increments the
             * SimpleAtomicLong mMaxIterations number of times.
             */
            new Thread(new RunTest(incrementCommand)).start();

            /**
             * Barrier synchronizer that waits for both worker threads
             * to exit before continuing.
             */
            mStopLatch.await();

            long result = mCounter.get();
            /**
             * Check to ensure the test worked, i.e., mCounter's value
             * should be 0.
             */
            if (result == 0)
                System.out.println("test worked");
            else
                System.out.println("test failed: mCounter = " + result);

            System.out.println("Finishing SimpleAtomicLongTest");
        }
        catch (Exception e) { }
    }
}




Java Source Code List

.BuggyLongTest.java
.Main.java
.PingPongWrong.java
.SimpleAtomicLongTest.java
.SimpleAtomicLong.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.AndroidPlatformStrategy.java
edu.vuum.mocca.BuggyLongTest.java
edu.vuum.mocca.BuggyLongTest.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.ConsolePlatformStrategy.java
edu.vuum.mocca.DownloadActivity.java
edu.vuum.mocca.DownloadActivity.java
edu.vuum.mocca.DownloadBase.java
edu.vuum.mocca.DownloadIntentService.java
edu.vuum.mocca.DownloadService.java
edu.vuum.mocca.DownloadUtils.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Main.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.Options.java
edu.vuum.mocca.PingPongActivity.java
edu.vuum.mocca.PingPongActivity.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PingPongRight.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategyFactory.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlatformStrategy.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.PlayPingPong.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongMultithreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongSingleThreadedTest.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLongTests.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleAtomicLong.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.SimpleSemaphore.java
edu.vuum.mocca.ThreadPoolDownloadService.java
edu.vuum.mocca.ThreadedDownloads.java
edu.vuum.mocca.provider.MoocSchema.java
edu.vuum.mocca.storage.MoocResolver.java
edu.vuum.mocca.storage.StorageUtilities.java
edu.vuum.mocca.storage.StoryCreator.java
edu.vuum.mocca.storage.StoryData.java
edu.vuum.mocca.ui.CreateStoryActivity.java
edu.vuum.mocca.ui.EditStoryActivity.java
edu.vuum.mocca.ui.ListStoryActivity.java
edu.vuum.mocca.ui.LoginActivity.java
edu.vuum.mocca.ui.SoundRecordActivity.java
edu.vuum.mocca.ui.StoryActivityBase.java
edu.vuum.mocca.ui.StoryDataArrayAdaptor.java
edu.vuum.mocca.ui.ViewStoryActivity.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.BuggyBlockingQueue.java
edu.vuum.mooca.SynchronizedQueueImpl.java
edu.vuum.mooca.SynchronizedQueueImpl.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueueTest.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java
edu.vuum.mooca.SynchronizedQueue.java.java