简单的基于Java名称的锁? [英] Simple Java name based locks?

查看:241
本文介绍了简单的基于Java名称的锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL有一个方便的功能:

MySQL has a handy function:

SELECT GET_LOCK("SomeName")

这可用于为应用程序创建简单但非常具体的基于名称的锁。但是,它需要数据库连接。

This can be used to create simple, but very specific, name based locks for an application. However, it requires a database connection.

我有很多情况,例如:

someMethod() {
    // do stuff to user A for their data for feature X
}

简单地同步此方法没有意义,因为,例如,如果同时为用户B调用此方法,则用户B不需要等待用户A完成它启动,只有用户A和功能X组合的操作需要等待。

It doesn't make sense to simply synchronize this method, because, for example, if this method is called for user B in the meantime, user B does not need to wait for user A to finish before it starts, only operations for the user A and feature X combination need to wait.

使用MySql锁我可以做类似的事情:

With the MySql lock I could do something like:

someMethod() {
    executeQuery("SELECT GET_LOCK('userA-featureX')")
    // only locked for user A for their data for feature X
    executeQuery("SELECT RELEASE_LOCK('userA-featureX')")
}

由于Java锁定是基于对象的,因此我似乎需要创建一个新对象来表示此锁的情况,然后将其放入静态缓存在某处,所以所有线程都可以看到它。锁定该情况的后续请求然后将锁定对象定位在高速缓存中并获取其锁定。我试图创建这样的东西,但锁缓存本身需要同步。此外,很难检测何时不再使用锁定对象,以便可以从缓存中删除它。

Since Java locking is based on objects, it seems like I would need to create a new object to represent the situation for this lock and then put it in a static cache somewhere so all the threads can see it. Subsequent requests to lock for that situation would then locate the lock object in the cache and acquire its lock. I tried to create something like this, but then the lock cache itself needs synchronization. Also, it is difficult to detect when a lock object is no longer being used so that it can be removed from the cache.

我查看了Java并发包,但没有什么能够处理这样的事情。有没有一种简单的方法来实现这一点,或者我是从错误的角度看待这个?

I have looked at the Java concurrent packages, but nothing stands out as being able to handle something like this. Is there an easy way to implement this, or am I looking at this from the wrong perspective?

编辑:

为了澄清,我不打算提前创建一个预定义的锁池,我想按需创建它们。我想的一些伪代码是:

To clarify, I am not looking to create a predefined pool of locks ahead of time, I would like to create them on demand. Some pseudo code for what I am thinking is:

LockManager.acquireLock(String name) {
    Lock lock;  

    synchronized (map) {
        lock = map.get(name);

        // doesn't exist yet - create and store
        if(lock == null) {
            lock = new Lock();
            map.put(name, lock);
        }
    }

    lock.lock();
}

LockManager.releaseLock(String name) {
    // unlock
    // if this was the last hold on the lock, remove it from the cache
}


推荐答案

这可能对您有用: jkeylockmanager

编辑:

我的初步反应可能有点短。我是作者,多次遇到这个问题,找不到现有的解决方案。这就是我在Google Code上创建这个小型库的原因。

My initial response was probably a bit short. I am the author and was faced with this problem several times and could not find an existing solution. That's why I made this small library on Google Code.

这篇关于简单的基于Java名称的锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆