1

Hazelcast 文档显示您可以使用分布式 Hazelcast 作为客户端,并提到如果持有锁的成员断开连接 - 锁将被释放。但是,如果我将锁用作客户端,这不会算作“成员” - 如果客户端断开连接,锁是否仍会安全释放?

4

1 回答 1

1

客户端锁将自动释放。试试下面的程序,你会看到自己。

public class Main {
    public static void main(String[] args) throws InterruptedException {
        HazelcastInstance hz1 = Hazelcast.newHazelcastInstance();
        HazelcastInstance client = HazelcastClient.newHazelcastClient();

        client.getLock("clientLock").lock();
        System.out.println("is locked:" + hz1.getLock("clientLock").isLocked());

        client.shutdown();

        System.out.println("Client is shutdown");
        for(;;){
            boolean isLocked = hz1.getLock("clientLock").isLocked();
            if(!isLocked){
                System.out.println("Client lock is released");
                return;
            }
            System.out.println("is locked:"+ isLocked);
            Thread.sleep(1000);
        }
   }
}
于 2013-11-05T05:43:06.077 回答