Java Programming Tutorials

Java programming tutorials with many code examples!

Java synchronized object

Problem:

How to guarantee thread-safely in concurrent Java code? In this post we’re going to show how to synchronize blocks of code using two different methods.

We are going to continue with the example from the previous post Java synchronized method, where a number of threads were calling dec() method on a Counter to bring it down to zero. Here’s the unsafe class from the previous example:

private static class Counter {
    private int number;

    public Counter(int number) {
        this.number = number;
    }

    public boolean dec() {
        // step 1: comparison
        if (number > 0) {
            // speed up unwanted modifications by other threads:
            Thread.yield();
            // step 2: modification
            --number;
            return true;
        }
        return false;
    }
}

Bad things happen when the Thread Scheduler mix threads between steps 1 and 2. To prevent that we can synchronize access on method level, which we’ve shown in the previous post.

Solution:

In this post we’re going to show how to do synchronized access in two other ways:

  • synchronized(this)
  • synchronized(lockObject).

The first solution is to use synchronize(this) to get the lock of the Counter object, so it’s very similar to what we did in the previous post, when we synchronized on method to get also Counter‘s lock. The difference is that synchronize(this) can be applied on blocks of code within a method, effectively holding lock for less time:

private static class Counter {
    private int number;

    public Counter(int number) {
        this.number = number;
    }

    public boolean dec() {
        // apply lock no block of code:
        synchronized(this) {
            if (number > 0) {
                Thread.yield();
                --number;
                return true;
            }
            return false;
        }
    }
}

In this case it’s almost the same as synchronized on method, because the block spans across whole method. But, of course, there could be more code outside of synchronized block.

In the second solution we use a bit different approach. We create a helper object that we’ll use only for obtaining its lock instead of Counter‘s own lock:

private static class Counter {
    // only for synchronization purposes:
    private final Object lockObject = new Object();

    private int number;

    public Counter(int number) {
        this.number = number;
    }

    public boolean dec() {
        synchronized(lockObject) {
            if (number > 0) {
                Thread.yield();
                --number;
                return true;
            }
            return false;
        }
    }
}

This solution looks similar to synchronized(this), because allows to obtain lock for blocks of code. The difference is that this lock cannot be obtained from outside of this class – for example by subclasses, or clients of the class – preventing denial-of-service attack (public lock could be held by prolonged period of time).

Share with the World!