Question

link

Consider the following class:

class MyCounter {
    private static int counter = 0;

    public static int getCount() {
        return counter++;
    }
}

Is the method thread-safe? How to make it thread-safe?

Solution

No, it’s not.

The method is not thread-safe, because the counter++ operation is not atomic, which means it consists more than one atomic operations. In this case, one is accessing value and the other is increasing the value by one.

When Thread 1 accesses the method at t1, Thread 2 may not be done with the method. So the value returned to Thread 1 is the value that has not been increased.

Approach 1

Adding synchronized to this method. This will synchronize the instance of the static class.

class MyCounter {
    private static int counter = 0;

    public static synchronized int getCount() {
        return counter++;
    }
}

Approach 2

We actually can make count++ atomic by using AtomicInteger from the package “java.util.concurrent.atomic”.

import java.util.concurrent.atomic.AtomicInteger;

public class MyCounter {
    private static AtomicInteger counter = new AtomicInteger(0);

    public static int getCount() {
        return counter.getAndIncrement();
    }
}

Follow-up on thread stack

  1. Each thread has its own stack (never share stack).
  2. All local variables defined in a method will be allocated memory in stack.
  3. When execution completed by a thread, stack frame will be removed.