Skip to content

Instantly share code, notes, and snippets.

@lanhuai
lanhuai / KeySynchronizer.java
Last active December 20, 2015 19:29
根据某个"key"获取一个同步子的工具类。内部使用Google guava-libraries 中的 MapMaker 创建的weakValues的ConcurrentMap保存同步子。以前同事是使用java.util.WeakHashMap实现,我这次改进为使用java.util.concurrent.ConcurrentMap实现,去掉了KeySynchronizer.acquire(String)方法上 原有的synchronized,希望能略微提高一点点性能。
import com.google.common.collect.MapMaker;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;