我应该同步CacheLoader.load()中的代码吗?
我有这样的代码:
final Calculator calculator = new Calculator();
final LoadingCache<Key, Value> cache = CacheBuilder.newBuilder().build(new CacheLoader<Key, Value>(){
@Override
public Value load(final Key key) throws Exception {
return calculator.calc(key);
}} );
Run Code Online (Sandbox Code Playgroud)
如果缓存需要从两个不同的线程加载两个不同键的值,我是否需要担心我的Calculator对象中的线程干扰?即我应该声明我的Calculator.calc()方法是同步的(或做其他事情以确保线程安全)?
编辑
就这样我很明确地询问Guava中的缓存问题:http://code.google.com/p/guava-libraries/wiki/CachesExplained