HCL Interview Question
Product Security EngineersCountry: India
Interview Type: In-Person
If only thread communication is the requirement and not mutual exclusion then making the shared object volatile would suffice.
There is one more mechanism which wasn't mentioned in the comments.
java.util.concurrent.Semaphore
Maintains multiple permits. The number of permits is determined by a constructor parameter.
Semaphore s = new Semaphore(permits)
When the parameter is set to 1 than Semaphore behaves as Lock.
We can use inbuilt lock provided by java.util.concurrent API
- Siddharth December 03, 2014Or
We can use atomic variable