Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 471
SoftMemes Avatar asked Jan 12 '23 23:01

SoftMemes


1 Answers

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(); 
like image 60
pveentjer Avatar answered Jan 30 '23 02:01

pveentjer