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

倾然丶 夕夏残阳落幕 提交于 2019-12-10 09:31:55

问题


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?


回答1:


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(); 


来源:https://stackoverflow.com/questions/19767328/in-hazelcast-is-it-possible-to-use-clustered-locks-that-do-not-care-about-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!