0

I implemented sample program to make sure that not more than 5 thread execute a particular process at a time. But the result is not as expected.

public class MiscUtils{

    private volatile static int count = 0;


    public synchronized static int getCount() {
        return count;
    }

    public synchronized static boolean incrementCounter() throws InterruptedException {
        System.out.println("count - " + count);
        while(MiscUtils.getCount() > 4) {
            System.out.println(Thread.currentThread().getName() + " Sleeping..");
            Thread.sleep(1000 * 5);
        }
        System.out.println("Thread acquired " + Thread.currentThread().getName());
        if(count > 4) {
            return false;
        }
        count++;
        return true;
    }

    public synchronized static boolean decrementCounter() {
        System.out.println("Decrement Called " + Thread.currentThread().getName());
        count--;
        return true;
    }


    public static void main(String[] args) throws InterruptedException {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    //System.out.println(new Date() + " :: Thread incremented");
                    MiscUtils.incrementCounter();
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                MiscUtils.decrementCounter();
                System.out.println(Thread.currentThread().getName());
            }

        };
        int count = 10;
        for(int i = 0; i < count; i++) {
            Thread s = new Thread(r);
            System.out.println(new Date() + "Thread " + i + " starting..");
            s.start();
            System.out.println(new Date() + "Thread " + i + " started..");
            Thread.sleep(1 * 1000);
        }

    }
}

Actual Output:

Mon Dec 30 19:29:20 IST 2019Thread 0 starting..
Mon Dec 30 19:29:20 IST 2019Thread 0 started..
count - 0
Thread acquired Thread-0
Mon Dec 30 19:29:21 IST 2019Thread 1 starting..
Mon Dec 30 19:29:21 IST 2019Thread 1 started..
count - 1
Thread acquired Thread-1
Mon Dec 30 19:29:22 IST 2019Thread 2 starting..
Mon Dec 30 19:29:22 IST 2019Thread 2 started..
count - 2
Thread acquired Thread-2
Mon Dec 30 19:29:23 IST 2019Thread 3 starting..
Mon Dec 30 19:29:23 IST 2019Thread 3 started..
count - 3
Thread acquired Thread-3
Mon Dec 30 19:29:24 IST 2019Thread 4 starting..
Mon Dec 30 19:29:24 IST 2019Thread 4 started..
count - 4
Thread acquired Thread-4
Mon Dec 30 19:29:25 IST 2019Thread 5 starting..
Mon Dec 30 19:29:25 IST 2019Thread 5 started..
count - 5
Thread-5 Sleeping..
Mon Dec 30 19:29:26 IST 2019Thread 6 starting..
Mon Dec 30 19:29:26 IST 2019Thread 6 started..
Mon Dec 30 19:29:27 IST 2019Thread 7 starting..
Mon Dec 30 19:29:27 IST 2019Thread 7 started..
Mon Dec 30 19:29:28 IST 2019Thread 8 starting..
Mon Dec 30 19:29:28 IST 2019Thread 8 started..
Mon Dec 30 19:29:29 IST 2019Thread 9 starting..
Mon Dec 30 19:29:29 IST 2019Thread 9 started..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread-5 Sleeping..

Expected Output -

Mon Dec 30 19:29:20 IST 2019Thread 0 starting..
Mon Dec 30 19:29:20 IST 2019Thread 0 started..
count - 0
Thread acquired Thread-0
Mon Dec 30 19:29:21 IST 2019Thread 1 starting..
Mon Dec 30 19:29:21 IST 2019Thread 1 started..
count - 1
Thread acquired Thread-1
Mon Dec 30 19:29:22 IST 2019Thread 2 starting..
Mon Dec 30 19:29:22 IST 2019Thread 2 started..
count - 2
Thread acquired Thread-2
Mon Dec 30 19:29:23 IST 2019Thread 3 starting..
Mon Dec 30 19:29:23 IST 2019Thread 3 started..
count - 3
Thread acquired Thread-3
Mon Dec 30 19:29:24 IST 2019Thread 4 starting..
Mon Dec 30 19:29:24 IST 2019Thread 4 started..
count - 4
Thread acquired Thread-4
Mon Dec 30 19:29:25 IST 2019Thread 5 starting..
Mon Dec 30 19:29:25 IST 2019Thread 5 started..
count - 5
Thread-5 Sleeping..
Mon Dec 30 19:29:26 IST 2019Thread 6 starting..
Mon Dec 30 19:29:26 IST 2019Thread 6 started..
Mon Dec 30 19:29:27 IST 2019Thread 7 starting..
Mon Dec 30 19:29:27 IST 2019Thread 7 started..
Mon Dec 30 19:29:28 IST 2019Thread 8 starting..
Mon Dec 30 19:29:28 IST 2019Thread 8 started..
Mon Dec 30 19:29:29 IST 2019Thread 9 starting..
Mon Dec 30 19:29:29 IST 2019Thread 9 started..
Thread-5 Sleeping..
Thread-5 Sleeping..
Thread acquired Thread-5
Thread-6 Sleeping..
Thread acquired Thread-6
and so on...

DecrementCounter() method is never being called. Also, I expected the synchronized block should update the count variable from getCount() method to take the latest value. But it is always returning value 5. Please let me know where i have gone wrong.

SaratBhaswanth
  • 340
  • 7
  • 18

2 Answers2

4

When you have synchronized static methods in class, it means that both are locking on the same monitor which is the class itself.

So when Thread-5 enters the while loop, it will hold the lock indefinitely while Threads 1-4 are still waiting to acquire the lock to enter decrementCounter.

Adwait Kumar
  • 1,552
  • 10
  • 25
  • I think I need to revisit java books... :D – SaratBhaswanth Dec 30 '19 at 14:32
  • Unrelated, but also an error: `MiscUtils.decrementCounter` would be called even if `MiscUtils.incrementCounter` returns false. – Alex B Dec 30 '19 at 14:35
  • Yes alex. Thats the exact place where I am struck. As @Adwait answered, `MiscUtils.incrementCounter` must be executed successfully so that `MiscUtils.decrementCounter` can be called.. – SaratBhaswanth Dec 30 '19 at 14:38
2

The main problems with your code are the following:

In Java, only one Thread can execute inside static synchronized method in the same class. [Reference]

  • This means that your threads will be holding the execution of MiscUtils.incrementCounter at the beginning.
  • It will also be blocking the execution of MiscUtils.decrementCounter.
  • After count reaches the value of 5, thread-5 will be stuck inside the the loop while(MiscUtils.getCount() > 4)
  • Then all the threads after thread-5 will be start, but will be put to sleep, since the synchronized static method MiscUtils.incrementCounter is still stuck with thread-5.

This will definitely cause an infinite loop.

I hope this was a clear answer.

Radwan Abu-Odeh
  • 1,897
  • 9
  • 16