In Hazelcast, is it possible to use clustered locks that do _not_ care about the local thread that performs the lock/unlock operations?

StackOverflow https://stackoverflow.com/questions/19767328

Question

Hazelcast locks (such as http://www.hazelcast.com/docs/1.9.4/manual/multi_html/ch02s07.html) as I understand it behave the same way as the Java concurrency primitives but across the cluster. The makes it possible to use to synchronize between thread in the local process as well as over the cluster.

However, is there any way I can opt out of this behaviour? In my current project, I need a way of coordinating unique ownership of a resource across the cluster but want to aquire and release this ownership from multiple points in my application - can I do this in some way that does not involve dedicating a thread to managing this lock in my process?

Était-ce utile?

La solution

The Semaphore is your friend because it doesn't have a concept of ownership. It uses permits that can be acquired; thread x can acquire permit 1, but thread y can release permit 1. If you initialize the semaphore with a single permit, you will get your mutual exclusion.

ISemaphore s = hazelcastInstance.getSemaphore("foo");
s.init(1);
s.acquire(); 

And in another thread you can release this permit by:

ISemaphore s = hazelcastInstance.getSemaphore("foo");
s.release(); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top