我有一个关于一致性锁定-解锁机制的测试示例,如下所示:
public class Test {
public static void main(String[] args) throws InterruptedException, IOException, IllegalArgumentException, IllegalAccessException {
Trt test=new Trt();
test.lock();
Thread a=new Thread(test);
a.start();
}
public static class Trt implements Runnable{
NamedCache cache=null;
@Override
public void run() {
System.out.println(cache.unlock("asd"));
}
public void lock(){
cache= CacheFactory.getCache(Globals.REGISTRY_CACHE_NAME);
System.out.println(cache.lock("asd"));
}
}
}
所以结果是:
true
false
我期望的结果是:
true
true
但情况是,我只有一项“测试”,我都在使用它,并且它只有一个缓存实例。所以缓存的所有者就是该缓存实例。
为什么无法关闭,最后返回false
?
谢谢
阿里
最佳答案
来自 Oracle Coherence 开发人员指南:
When a lock is in place, it is the responsibility of the caller (either in the same thread or the same cluster node, depending on the lease-granularity configuration) to release the lock.
默认情况下,Coherence 使用线程所有权粒度,因此这可能是锁未释放的原因。
A value of thread means that locks are held by a thread that obtained them and can only be released by that thread. A value of member means that locks are held by a cluster node and any thread running on the cluster node that obtained the lock can release it.
参见http://docs.oracle.com/cd/E24290_01/coh.371/e22837/api_transactionslocks.htm#BEIIEEBB和 http://docs.oracle.com/cd/E24290_01/coh.371/e22837/appendix_operational.htm#BAGJBCEF了解更多详情。
关于java - 一致性锁定解锁用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009430/