Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:47:35+00:00 2026-06-04T03:47:35+00:00

The following concurrency code, made using Java’s Semaphore class, enters a deadlock, even tough,

  • 0

The following concurrency code, made using Java’s Semaphore class, enters a deadlock, even tough, as per console output, the permit is being released.

package ThreadTraining;

import java.util.concurrent.Semaphore;

public class ThreadTraining {

    public static class Value {

        private static int value = 0;
        private static final Semaphore SEMAPHORE = new Semaphore(1);

        public static synchronized void acquire() throws InterruptedException {
            SEMAPHORE.acquire();
            System.out.println("A thread has aquired a permit!");
        }

        public static synchronized void release() {
            SEMAPHORE.release();
        }

        public static int get() {
            return value;
        }

        public static void add() {
            value++;
        }

        public static void subtract() {
            value--;
        }
    }

    public static class Adder extends Thread {

        public Adder(String name) {
            this.setName(name);
        }

        @Override
        public void run() {
            System.out.println(this.getName() + " has been created.");
            boolean keepRunning = true;
            while (keepRunning) {
                try {
                    Value.acquire();
                    System.out.print(this.getName() + " has aquired Value's permit. --- ");
                    if (Value.get() > 99) {
                        System.out.print(this.getName() + " has finished it's job. --- ");
                        keepRunning = false;
                    } else {
                        System.out.print(this.getName() + " has modified value from " + Value.get() + "  to ");
                        Value.add();
                        System.out.println(Value.get() + ".");
                    }
                } catch (InterruptedException ie) {
                    System.err.println("This thread was interrupted.");
                } finally {
                    System.out.println(this.getName() + " is releasing Value's permit.");
                    Value.release();
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread threads[] = new Thread[3];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Adder("[Adder]Thread #" + i);
        }
        for (Thread t : threads) {
            t.start();
        }
    }
}

The code’s console output: (This was a “lucky” run, it usually only prints up to the indicated point)

[Adder]Thread #0 has been created.
[Adder]Thread #1 has been created.
[Adder]Thread #2 has been created.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 0  to 1.
[Adder]Thread #0 is releasing Value's permit. /*NOTE: It usually prints only up to this line, hanging after the first permit-release.*/
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 1  to 2.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 2  to 3.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 3  to 4.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 4  to 5.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 5  to 6.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 6  to 7.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 7  to 8.
[Adder]Thread #0 is releasing Value's permit.

What are the reasons behind it? And, if possible, how to fix it?


Additional info:

This question is a “continuation” of my previous concurrency question.

The new code is very based on this semaphore tutorial.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:47:36+00:00Added an answer on June 4, 2026 at 3:47 am

    The problem is the synchronization of your own methods. Both Value.acquire and Value.release are synchronized, so one thread entering your acquire-method will block another one from calling release, because the release call will wait for the Value-class’s monitor to be released, while the one inside acquire will wait for the internal Semaphore to be acquired. Remove the synchronized -keywords from your methods and you’ll get rid of the deadlock-problem. Instead, you probably meant to synchronize your get-, add– and subtract-methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Java code: import java.util.concurrent.*; class Foo{ static Semaphore s =
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
I have written the following code: import java.util.Calendar; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; class Voter
Reading this DZone article about Java concurrency I was wondering if the following code:
The following code has concurrency issues. Here inDegVec is a global int * ,
Following this tutorial (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application), I learned how to save data and do concurrency checks
I have the following code running, but I sometimes get some sort of concurrency
How would you solve the concurrency issue with the following code? In this example,
I'm loading jQuery via Google's CDN using the following code. My main question is
I have the following code in a collection: class Author(Agent): def foo(self): self.find_another_document_and_update_it(ids) self.processed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.