从多个线程进行Ehcache并发读写

时间:2018-10-30 16:17:15

标签: multithreading concurrency thread-safety ehcache

当Thread1将新的键/值放入缓存中并且Thread2尝试读取它时,我在访问Ehcache时遇到问题。由Thread2读取不会返回Thread1插入的值。

请注意,第一种方法是通过启动应用程序来调用的,第二种方法几乎是由REST控制器同时调用的。同样在下面的示例中,我正在使用BlockingCache,但也已使用简单的缓存对其进行了测试。还测试了显式锁定。

看起来像这样:

private void processThread1(Object object) {
    BlockingCache blockingCache = new BlockingCache(getCache());
    blockingCache.put(new Element(object.getKey(), object));
}

private void processThread2(Object object) {
    BlockingCache blockingCache = new BlockingCache(getCache());
    // this value is not updated by processThread1(..) when
    // it's accessed, although processThread1(..) method call 
    // finishes before this method is called
    SomeObject someObject = blockingCache.get(object.getKey());
}

Cache getCache() {
    return cacheManager.getCache(cacheName);
}

0 个答案:

没有答案