Java synchronized method around parameter value

前端 未结 2 1580
梦毁少年i
梦毁少年i 2021-01-06 01:57

Consider the following method:

public void upsert(int customerId, int somethingElse) {
  // some code which is prone to race conditions    
}
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 02:07

        private static final Set lockedIds = new HashSet<>();
    
        private void lock(Integer id) throws InterruptedException {
            synchronized (lockedIds) {
                while (!lockedIds.add(id)) {
                    lockedIds.wait();
                }
            }
        }
    
        private void unlock(Integer id) {
            synchronized (lockedIds) {
                lockedIds.remove(id);
                lockedIds.notifyAll();
            }
        }
    
        public void upsert(int customerId) throws InterruptedException {
            try {
                lock(customerId);
    
                //Put your code here.
                //For different ids it is executed in parallel.
                //For equal ids it is executed synchronously.
    
            } finally {
                unlock(customerId);
            }
        }
    
    • id can be not only an 'Integer' but any class with correctly overridden 'equals' and 'hashCode' methods.
    • try-finally - is very important - you must guarantee to unlock waiting threads after your operation even if your operation threw exception.
    • It will not work if your back-end is distributed across multiple servers/JVMs.

提交回复
热议问题