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

  • SEARCH
  • Home
  • 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 6849599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:00:42+00:00 2026-05-27T01:00:42+00:00

I need clarification regarding an issue related to multithreading. I have threads which acquires

  • 0

I need clarification regarding an issue related to multithreading. I have threads which acquires a semaphore, and after a while releases it. As soon as it is done releasing first semaphore, it acquires second one and after while releases it. Both semaphore protects different code in thread’s run() method. Something like below:

public void run() {
     System.out.println("Step 1");
     semaphoreA.acquire();

     // Run for a while

     semaphoreA.release();

     // Run for a while

     semaphoreB.acquire();
     System.out.println("Step 2");

     // Run for a while

     semaphoreB.release();
}

So, when there is no permit for semaphoreB, thread waits. However, when the permit made available, shouldn’t I see ‘Step 2’ on console ? Or that is how thread and semaphore works ?

I want to understand what happens if a thread was just notified about an available semaphore permit. Would that thread start from the beginning ? or From the point where it was left of ?

  • 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-05-27T01:00:42+00:00Added an answer on May 27, 2026 at 1:00 am

    Assuming that you instantiated semaphoreA and SemaphoreB with new Semaphore(1, true) at best you will see:

    Step 1

    Step 2

    If some other thread acquires the semaphore, until it is released, this thread just waits where it is. In other words, you will never Step 1 once, but never twice. You may or not see Step 2 depending if any of the semaphores is released.

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

Sidebar

Related Questions

I need some clarification regarding runtime/heap issues when deleting an object which was created
I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
I looking for a clarification regarding the pointers. I have compiled the following code
I need some clarification/advice regarding modifying objects stored in mutable containers in Objective-C. Let's
I'm looking for some clarification regarding the behaviour of redirect_to . I have this
I have 2 questions or rather clarifications I need related to CSS overflow property..It
i have some clarification regarding Qt programming. In Qt most of the time we
I need some help for iphone application,i have some question regarding the new iOS
I just need clarification on what a managed prototype is. I think it is
I just need some clarification on variables A normal variable has 2 parts to

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.