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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:05:18+00:00 2026-06-16T18:05:18+00:00

I am on my way learning Java multithread programming. I have a following logic:

  • 0

I am on my way learning Java multithread programming. I have a following logic:

Suppose I have a class A

class A {
    ConcurrentMap<K, V> map;

    public void someMethod1 () {
        // operation 1 on map
        // operation 2 on map
    }

    public void someMethod2 () {
        // operation 3 on map
        // operation 4 on map
    }
}

Now I don’t need synchronization of the operations in “someMethod1” or “someMethod2”. This means if there are two threads calling “someMethod1” at the same time, I don’t need to serialize these operations (because the ConcurrentMap will do the job).

But I hope “someMethod1” and “someMethod2” are mutex of each other, which means when some thread is executing “someMethod1”, another thread should wait to enter “someMethod2” (but another thread should be allowed to enter “someMethod1”).

So, in short, is there a way that I can make “someMethod1” and “someMethod2” not mutex of themselves but mutex of each other?

I hope I stated my question clear enough…

Thanks!

  • 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-16T18:05:21+00:00Added an answer on June 16, 2026 at 6:05 pm

    I tried a couple attempts with higher-level constructs, but nothing quite came to mind. I think this may be an occasion to drop down to the low level APIs:

    EDIT: I actually think you’re trying to set up a problem which is inherently tricky (see second to last paragraph) and probably not needed (see last paragraph). But that said, here’s how it could be done, and I’ll leave the color commentary for the end of this answer.

    private int someMethod1Invocations = 0;
    private int someMethod2Invocations = 0;
    
    public void someMethod1() {
        synchronized(this) {
            // Wait for there to be no someMethod2 invocations -- but
            // don't wait on any someMethod1 invocations.
            // Once all someMethod2s are done, increment someMethod1Invocations
            // to signify that we're running, and proceed
            while (someMethod2Invocations > 0)
                wait();
            someMethod1Invocations++;
        }
    
        // your code here
    
        synchronized (this) {
            // We're done with this method, so decrement someMethod1Invocations
            // and wake up any threads that were waiting for that to hit 0.
            someMethod1Invocations--;
            notifyAll();
        }
    }
    
    public void someMethod2() {
        // comments are all ditto the above
        synchronized(this) {
            while (someMethod1Invocations > 0)
                wait();
            someMethod2Invocations++;
        }
    
        // your code here
        synchronized(this) {
            someMethod2Invocations--;
            notifyAll();
        }
    }
    

    One glaring problem with the above is that it can lead to thread starvation. For instance, someMethod1() is running (and blocking someMethod2()s), and just as it’s about to finish, another thread comes along and invokes someMethod1(). That proceeds just fine, and just as it finishes another thread starts someMethod1(), and so on. In this scenario, someMethod2() will never get a chance to run. That’s actually not directly a bug in the above code; it’s a problem with your very design needs, one which a good solution should actively work to solve. I think a fair AbstractQueuedSynchronizer could do the trick, though that is an exercise left to the reader. 🙂

    Finally, I can’t resist but to interject an opinion: given that ConcurrentHashMap operations are pretty darn quick, you could be better off just putting a single mutex around both methods and just being done with it. So yes, threads will have to queue up to invoke someMethod1(), but each thread will finish its turn (and thus let other threads proceed) extremely quickly. It shouldn’t be a problem.

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

Sidebar

Related Questions

i am learning Java at the moment and have the following question: i am
I am on my way learning multi-thread programming with Java. Here is a confusion
I'm learning my way around Java JTree's but I have this little problem I
Im learning Java and having a problem with ArrayList. Firstly I have a class
I have just began learning Java programming, I have created a simple game using
I'm hacking my way through learning Flex and have found some strange behaviour. When
I was planning on learning a way to create my own programming language and
I'm learning socket programming (in python) and I was wondering what the best/typical way
I'm learning Java and OOP, and have been doing the problems at Project Euler
I've downloaded the JAVA Eclipse IDE and started learning Java. I have a good

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.