Over a million developers have joined DZone.

How to Test if Your Multi-Threaded Java Rest Service Is Thread Safe

Thomas Krieger shows how to test if your rest service is thread safe by analyzing a counter with a race condition and using a multi-threaded test runner.

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

In the following article, you will see how to test if your rest service is thread safe. Let us start with a simple example—a counter which contains a race condition.

The Not-Thread Safe Counter

As an example, we use the following jersey rest service. It consists of a resource that increments a counter for each post call and returns the new value:

public class Counter {

     private static int i = 0;

    public String addOne() {

    return new Integer(i++).toString();

This is clearly not thread safe. The access to the variable counter is not synchronized, which will lead to a race condition, if the method “addOne” is called from too many threads in parallel. Let us see, if we can detect this bug with a test.

The Test

To test if this service is thread safe, we need a multi-threaded test, like the following:

public class CounterTest {

    private HttpServer server;
    private WebTarget target;

    public void setUp() throws Exception {
        server = Main.startServer();
        Client c = ClientBuilder.newClient();
        target = c.target(Main.BASE_URI);

    public void tearDown() throws Exception {

    public void testAddOne() {

    String responseMsg = target.path("counter").request().post(Entity.json(null) , String.class);
     * Checking the responseMsg left out for brevity...

The concurrent test runner runs the test method in parallel with THREAD_COUNT threads. To detect race conditions, we need a tool, which can detect race conditions during tests. One such tool is vmlens. We can enable it by adding the vmlens agent path to the vm arguments. After running, we will see the race condition in vmlens:

Race Condition in REST Service

After we have found the race condition, we want to fix the race.

Making the Rest Service Thread Safe

The easiest way to do this is to use a java.util.concurrent.atomic.AtomicInteger. AtomicInteger uses a volatile field internally, making updates visible to all threads. And the used method “addAndGet” is made atomic by using compareAndSet.

public class Counter {

     private static AtomicInteger i = new AtomicInteger();

    public String addOne() {

    return new Integer(i.incrementAndGet()).toString();


To test a multi threaded java rest service, we needed two things—a multi-threaded test runner and a tool that can detect Java race conditions. For the multi-threaded test I used concurrent-junit, for the race condition detection I used vmlens. If you have a question or remark, please add a comment in the comments section.

Building Identity Management, including authentication and authorization? Try Stormpath! Our REST API and robust Java SDK support can eliminate your security risk and can be implemented in minutes. Sign up, and never build auth again!


Published at DZone with permission of Thomas Krieger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}