如何在这种情况下添加锁?

Bob*_*ang 4 java multithreading

像这样的代码......

public void beforeUpdated(Log log){
    synchronized(this){
       query(log);
       merge(log);
       persist(log);
    }
}
Run Code Online (Sandbox Code Playgroud)

此方法在多线程环境下运行.日志的CRUD必须是原子操作.但只需要同步相同id的日志(log.getUuid()).如果我锁定所有操作,它必须是一个性能不佳.我只希望同一个id的日志在原子操作下锁定.我该怎么做?如果你有任何想法,请帮帮我,谢谢.

ida*_*alz 5

我几次遇到这种情况.你需要的是一个单独的LockFactory,它实际上是一个锁对象弱引用的字典.代码应该是这样的:

class LockFactory {
    private LockFactory() {}
    private LockFactory instance = null;
    public static LockFactory getInstance() { 
        if (this.instance == null)
            this.instance = new LockFactory();
        return this.instance;
    }
    private int _last_check_size = 0;
    private int _cleanup_size = 1000;
    private Map<String, WeakReference> weakRefDictionary = new HashMap<String, WeakReference>();
    public object getLock(String id) {
        synchronized(this) {
             if (!this.weakRefDictionary.containsKey(id))
                 this.weakRefDictionary.put(id, new WeakReference(null));
             Object lock = this.weakRefDictionary.get(id).Target;
             if (lock == null) { 
                lock = new Object();
                this.weakRefDictionary.get(id).Target = lock;
             }
             if (this.weakRefDictionary.size() > this._last_check_size + this._cleanup_size)
                  this._do_cleanup();
             return lock;
        }
    }
    public void _do_cleanup() {
        synchronized(this) {
            Iterator<Map.Entry<String, WeakReference>> iter = this.weakRefDictionary.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String,WeakReference> entry = iter.next();
                if (entry.getValue().get() == null) {
                    iter.remove();
                }
            }
            this._last_check_size = this.weakRefDictionary.size();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在你的情况下使用只需:

public void beforeUpdated(Log log){
    synchronized(LockFactory.getInstance().getLock(log.getUuid())){
       query(log);
       merge(log);
       persist(log);
    }
}
Run Code Online (Sandbox Code Playgroud)