0

I have a requirement in java to write a method which will be accessed by multiple threads simultaneously. But it should allow only one thread per customer at a time. Means any different customer threads can work same time. Customers are separated by customer id, which is available as an argument to the function.

What is the best way to achieve this? Its somewhat similar to implementing a semaphore but the counter should be on a field/variable value.

Sreehari
  • 9
  • 3
  • You could manipulate a concurrent set and only execute the body of the method if the customer ID can be added to it (meaning it's not there already), then remove it from the set at the end of the method's implementation. – Mena May 14 '18 at 14:42
  • 2
    Possible duplicate of [Simple Java name based locks?](https://stackoverflow.com/questions/5639870/simple-java-name-based-locks) – Ruslan Akhundov May 14 '18 at 14:46

0 Answers0