简体   繁体   English

Hazelcast Trylock 和多次通话

[英]Hazelcast Trylock and multiple calls

I ran into the following issue:我遇到了以下问题:

We have a multipart Process P with two steps ( s1 and s2 ).我们有一个多部分流程P有两个步骤( s1s2 )。 The process is implemented, that in s1 a lock is acquired - but not released.该过程已实现,即在s1中获取锁 - 但未释放。 In s2 the lock is required again (?) and after s2 is done, the lock is released again.s2 中再次需要锁(?),在s2完成后,再次释放锁。

From the documentation :文档

Locks are re-entrant: the same thread can lock multiple times on the same lock.锁是可重入的:同一个线程可以在同一个锁上多次锁定。 Note that for other threads to be able to require this lock, the owner of the lock must call unlock as many times as the owner called lock.请注意,要让其他线程能够要求此锁,锁的所有者必须调用 unlock 的次数与所有者调用 lock 的次数相同。

If I understand this correctly, this means, if I call s1 for the first time, the lock is acquired by a Thread from the threadpool, say Tx .如果我理解正确,这意味着,如果我第一次调用s1 ,锁是由线程池中的线程获取的,比如Tx Since the lock is not released after the request, the thread goes back to the pool.由于请求后没有释放锁,线程回到池中。 If a second request comes in, the lock is in use (by Tx ).如果第二个请求进来,则锁正在使用中(通过Tx )。 But if I start enough requests, at some point in time, I will get Tx from the pool again, and I am able to successfully tryLock the same lock as before again?但是,如果我开始足够的请求,在某个时间点上,我会从池中再次获得的Tx,我能够成功的tryLock相同的锁的前一次?

Is this right?这是正确的吗? This means, the lock is not exclusive for it's lifetime, or only insofar exclusive, that it is exclusively bound to one thread.这意味着,锁在它的生命周期内不是独占的,或者只是在独占的范围内,它被独占地绑定到一个线程。

A lock has the concept of lock ownership;锁具有锁所有权的概念; which means that there is a particular thread is owning a lock.这意味着有一个特定的线程拥有锁。 It also means that only this thread needs to release this lock.这也意味着只有这个线程需要释放这个锁。

So your usecase where thread1 acquires and thread2 releases, will not work with a lock (no matter if it a hz lock or a regular juclReentrantLock).因此,您的线程 1 获取和线程 2 释放的用例将无法使用锁(无论是 hz 锁还是常规的 juclReentrantLock)。

You might want to have a look at the ISemaphore and initialise it with 1, so you can create a binary semaphore.您可能想查看 ISemaphore 并将其初始化为 1,这样您就可以创建一个二进制信号量。 This will allow you to acquire a 'lock' in 1 thread and release the 'lock' in another.这将允许您在 1 个线程中获取“锁”并在另一个线程中释放“锁”。

However, it will not deal with reentrant behavior.但是,它不会处理可重入行为。 But will deadlock instead.反而会死锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM