all that jazz

james' blog about scala and all that jazz

Java Concurrency and Volatile

The volatile keyword is a keyword that very few Java developers know the meaning of, let alone when they should use it. The reason for this, I believe, is that the reason why it's needed is such a complex topic that unless you've studied in detail the way CPUs use registers, cache, and the way the JVM uses stack frames, it's impossible to understand why it's needed. The other reason I think, is that it is difficult to demonstrate the consequences of not using it. That is why I came up with this little puzzle, to highlight how important the volatile keyword is.

For this demonstration, you will need a multi processor Linux 2.6 or OpenSolaris system, with Java 5 or above. It will not work on Mac or Windows. If you know why it doesn't work on Mac or Windows, please leave a comment explaining, I'd really like to know. What this does highlight though is just how complex Java concurrency issues are.

So on to the puzzle. Without executing it, try and work out what will happen when you run the following code:

public class ConcurrencyFun implements Runnable
{
    private String str;
    void setStr(String str)
    {
        this.str = str;
    }
    public void run()
    {
        while (str == null);
        System.out.println(str);
    }
    public static void main(String[] args) throws Exception
    {
        ConcurrencyFun fun = new ConcurrencyFun();
        new Thread(fun).start();
        Thread.sleep(1000);
        fun.setStr("Hello world!!");
    }
}

Most people would guess that the above code would wait for about one second, print the text "Hello world!!", and then exit. The spawned thread busy waits for str to not be null, and then prints it. The main thread, after starting the spawned thread, waits for one second, and then sets str to be "Hello world!!". Simple, right?

Now try running it (remember, only on a multi processor Linux 2.6 or Solaris system). What actually happens? On my machine, the program never exits. Why is this?

The reason is that the JVM is free to make its own copy of the str pointer available to each thread that uses it. This could come in many forms. It could be that the pointer is loaded into a register and is continually read from that register. This is what is most likely happening in our case. It could be that the pointer is loaded into the CPU cache, and never expired, even after update. Or, it is also possible that the JVM will make a copy of the pointer in the threads stack frame, to allow for more efficient memory access. Whether you understand anything I've just said or not, the point is that changes to the str field may not necessarily be seen by all threads accessing it, in our case, it will never be seen by the spawned thread.

This is where the volatile keyword comes in. The volatile keyword tells the JVM that any writes to that field must be viewable by all threads. This means that the compiled machine code may not read the variable into a register and use that multiple times, it must read it from memory every time. It also must not read it from the CPU cache, it must make sure that every read comes straight from memory. And finally, it stops the JVM from creating a local copy of the field in the threads stack frame.

So, adding the volatile keyword, like so:

public class ConcurrencyFun implements Runnable
{
    private volatile String str;
    void setStr(String str)
    {
        this.str = str;
    }
    public void run()
    {
        while (str == null);
        System.out.println(str);
    }
    public static void main(String[] args) throws Exception
    {
        ConcurrencyFun fun = new ConcurrencyFun();
        new Thread(fun).start();
        Thread.sleep(1000);
        fun.setStr("Hello world!!");
    }
}

results in the expected behaviour happening, the program waits one second, prints out "Hello world!!" and then exists.

The complexity of concurrency

There are other ways to make the above code work. For example, if in the while loop, you add some code that prints something out, you will find that it works. My guess at the reason for this is that the register storing str ends up getting used for something else, and so on each iteration, str gets read from memory. Note that this is not a real fix, it is still possible for problems to occur, and indeed on some architectures the program still will not exit. Another thing that will work is to invoke Java with the -Xint argument. This disables machine code compilation, and hence makes concurrency issues arising from registers and CPU caches much less likely. But again, it's not a solution. Using the volatile keyword is the only solution that guarantees that it will work, every time, on every platform.

Atlassian Stimulus Package

For five days only, you can buy 5 user licenses of the full versions of JIRA and Confluence for only $5. This includes full support for a year, and renewal will only ever cost you $5. Furthermore, all revenue raised from the promotion goes to charity. What a great way for small teams to sneak Confluence and JIRA into their companies! It's also great for people that want them for personal use but would like a few more than 2 users.

Copy and paste between Firefox and the iPhone

Today I decided to have a go at diagnosing why the reversing lights on my car weren't working. I quickly worked out that the bulbs were fine, so I jumped on Google to see if I could find some information about where else problems may occur with reversing lights in a car. I found some very detailed instructions, but I had a problem. These instructions were on my computer, in my room, but I wanted to take them to my car, in my garage, and I don't have a printer. However, I do have an iPhone, so I thought I'd copy the URL into my iPhone and read the instructions from Safari. The URL however was rather long and copying by hand would have been painful, what I really wanted to do was the equivalent of copy and paste from my computer to my iPhone.

Enter Mobile Barcoder. Mobile Barcoder is a Firefox extension that allows you to generate QR Codes from Firefox. A QR Code is a 2D barcode designed to be read particularly by mobile devices. Using Mobile Barcoder, generating a barcode for the current page is as simple as hovering your mouse over the icon it puts in the bottom right corner of the window:

Generating a barcode from the current page

You can also right click on a link to generate a barcode for that link:

Generating a barcode from a link

You can even create a barcode from arbitrary text on the page, by selecting it and then right clicking:

Generating a barcode from selected text

So I have my barcode, but what use is that to me? Enter BeeTag. BeeTag is a free iPhone App that reads QR Codes and other 2D barcodes, and, depending on the type of code, lets you act on it accordingly. For example, if it's a phone number, you can dial the number, if it's a URL, you can open it in Safari, or if it's plain text, you can save it as a memo. Scanning is as easy as taking a picture:

Taking a picture of a barcode with BeeTag

Having taken the picture, it will read it, and then prompt you for what you want to do next. I chose to open the URL in Safari:

BeeTag prompt after successfully reading a barcode The webpage on my iPhone in Safari

Now all I need to do is wait for the iPhone 3.0 firmware with copy and paste, and I have full end to end copy/paste from my computer to my iPhone.

About

Hi! My name is James Roper, and I am a software developer with a particular interest in open source development and trying new things. I program in Scala, Java, PHP, Python and Javascript, and I work for Lightbend as a developer on Lagom. I also have a full life outside the world of IT, am a passionate Christian, enjoy playing a variety of musical instruments and sports, and currently I live in Canberra.

I also have a another blog called Roped In about when my wife and I lived in Berlin for a year to help a church reconnect with its city.