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 8819067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:15:39+00:00 2026-06-14T05:15:39+00:00

I have two methods: a() and b() . While I’m fine with multiple threads

  • 0

I have two methods: a() and b(). While I’m fine with multiple threads accessing any of the methods at the same time (that is desirable), I do not want any threads to enter a() while b() is being executed.
How do I do that?

Edit 1

Lets say there are 4 threads and Thread 1 is accessing A(). What I want is all of the 4 threads should not use B().

  • 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-14T05:15:40+00:00Added an answer on June 14, 2026 at 5:15 am

    CHECK UPDATE at the bottom – I don’t think this approach can work. Leaving it for information.


    You could use a Semaphore:

    • if a thread is in b(), a thread trying to execute a() will block until the execution of b() is over.
    • if a thread is in b() and a second thread tries to run b() it will be able to
    • if 2 threads execute a() while b() is not being executed both will run

    Note: once a thread is in a() and has passed the “semaphore test”, another thread can start running b() before the end of a()

    The principle should work but I have not tested it.

    private final Semaphore inB = new Semaphore(1);
    
    public void a() throws InterruptedException {
        inB.acquire(); //blocks until no thread is in b any longer
        //now we are good to go and execute a()
        //release the semaphore immediately for other threads who want to run a()
        inB.release(); 
        //rest of your code here
    }
    public void b() {
        //does not block to allow 2 thread running b() simultaneously
        boolean needToRelease = inB.tryAcquire(); 
        //rest of your code
        //if the permit was acquired, release it to allow threads to run a()
        if (needToRelease) {
            inB.release();
        }
    }
    

    EDIT

    Your intention is not clear and your question has been edited because one of your comments says that you want a() and b() to be mutually exclusive (many threads can run either a() or b() in parallel, but a() and b() should never be run in parallel). In that case, you can use the same logic with 2 semaphores inA and inB.


    UPDATE => BUG

    As pointed out by @yshavit in a comment to another answer, there is a race condition in the code in the following scenario:

    • T1 runs a() and acquires inB
    • T2 runs b() and fails to acquire inB
    • T1 releases inB
    • T3 runs a() and manages to acquire inB although T2 is running b()

    It seems that this can’t be achieved with Sempahores only. This answer gives a better solution.

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

Sidebar

Related Questions

Suppose I have two methods, Foo and Bar , that do roughly the same
Let's say you have two different C# classes A and B that while not
I have two methods, one that I use to convert an image to a
Is it possible to have two methods with the same name but different parameters
I have a class that I have to call one or two methods a
I have two methods, for example, delayloop() and function(), where delayloop() has a while
I have two methods that I'm using as custom tags in a template engine:
Assuming I have two methods. One that creates an arraylist and returns it, the
assume that i have two sequential methods, methodA and methodB ( methodB is called
I have two projects that contain extension methods. One project is implemented using C#

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.