Threads synchronization on a non-volatile vs volatile boolean flag and AtomicBoolean

1.

public class ThreadsSynchronizationOnBooleanFlag {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadsSynchronizationOnBooleanFlag.class);

    static boolean done = false;

    public static void main(String[] args) throws InterruptedException {
        LOGGER.debug("started");

        new Thread(new Runnable() {
             
            @Override
            public void run() {
                LOGGER.debug("started");
                while (!done) {}
                LOGGER.debug("stopped");
            }
        }).start();

        Thread.sleep(500); // give additional time to make sure thread-0 started already
        done = true;
        LOGGER.debug("stopped");
    }
}

/*
2015-10-22 12:19:48,794|main    |started
2015-10-22 12:19:48,794|Thread-0|started
2015-10-22 12:19:49,309|main    |stopped
*/

Explanation: Thread-0 still running as thread0 does not see flag change done by main thread.

2.

public class ThreadsSynchronizationOnVolatileBooleanFlag {
    private static final Logger LOGGER = 
           LoggerFactory.getLogger(ThreadsSynchronizationOnVolatileBooleanFlag.class);

    static volatile boolean done = false;

    public static void main(String[] args) throws InterruptedException {
        LOGGER.debug("started");

        new Thread(new Runnable() {

            @Override
            public void run() {
                LOGGER.debug("started");
                while (!done) {
                }
                LOGGER.debug("stopped");
            }
        }).start();

        Thread.sleep(500); // give additional time to make sure thread-0 started already
        done = true;
        LOGGER.debug("stopped");
    }
}
/*
2015-10-22 12:24:18,369|main    |started
2015-10-22 12:24:18,369|Thread-0|started
2015-10-22 12:24:18,883|main    |stopped
2015-10-22 12:24:18,883|Thread-0|stopped
*/

Explanation: volatile flag makes all threads read/write directly into RAM main memory so the thread-0 can see value change done by the main thread.

3.

public class ThreadsSynchronizationOnAtomicBooleanFlag {
    private static final Logger LOGGER = 
             LoggerFactory.getLogger(ThreadsSynchronizationOnAtomicBooleanFlag.class);

    static AtomicBoolean done = new AtomicBoolean(false);

    public static void main(String[] args) throws InterruptedException {
        LOGGER.debug("started");

        new Thread(() -> {
            LOGGER.debug("started");
            while (!done.get()) {}
            LOGGER.debug("stopped");
        }).start();

        Thread.sleep(500); // give additional time to make sure thread-0 started already
        done.set(true);
        LOGGER.debug("stopped");
    }
}
/*
2015-10-22 12:35:57,738|main    |started
2015-10-22 12:35:57,800|Thread-0|started
2015-10-22 12:35:58,315|main    |stopped
2015-10-22 12:35:58,315|Thread-0|stopped
*/

Explanation: atomic boolean internally declares boolean as volatile so the thread-0 can see value change done by the main thread.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s