Skip to content

Instantly share code, notes, and snippets.

@kwon37xi
Created October 10, 2013 16:22
Show Gist options
  • Save kwon37xi/6921207 to your computer and use it in GitHub Desktop.
Save kwon37xi/6921207 to your computer and use it in GitHub Desktop.
Google Guava cache에서 캐시의 특정 키에 값이 없는 상태에서 여러 쓰레드에서 요청이 들어와도 최초 요청 쓰레드 하나만 값을 가져오는 코드를 실행하고, 나머지는 기다렸가가 캐시의 결과를 리턴 받는 것을 시연한다.
package kr.pe.kwonnam.guavatest.cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* Google Guava cache에서 캐시의 특정 키에 값이 없는 상태에서 여러 쓰레드에서
* 요청이 들어와도 최초 요청 쓰레드 하나만 값을 가져오는 코드를 실행하고,
* 나머지는 기다렸가가 캐시의 결과를 리턴 받는 것을 시연한다.
* <p/>
* Author: KwonNam Son(kwon37xi@gmail.com)
*/
public class CacheLockTest {
public static void main(String[] args) throws Exception {
LoadingCache<String, String> cache = CacheBuilder.newBuilder().maximumSize(5).expireAfterWrite(10, TimeUnit.MINUTES).build(new CacheLoader<String, String>() {
@Override
public String load(String key) throws Exception {
for (int i = 1; i <= 10; i++) {
System.out.println("sleeping " + i + " times " + Thread.currentThread().getName());
Thread.sleep(1000);
}
return "I'm fine, thank you, and you?";
}
});
for (int i = 0; i < 10; i++) {
new Thread(new CacheAccessor(cache)).start();
Thread.sleep(200);
}
}
private static class CacheAccessor implements Runnable {
private LoadingCache<String, String> cache;
public CacheAccessor(LoadingCache<String, String> cache) {
this.cache = cache;
}
@Override
public void run() {
try {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " request a value to the cache.");
String result = cache.get("How are you?");
System.out.println("Thread " + threadName + " got answer \"" + result + "\".");
} catch (ExecutionException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}
@kwon37xi
Copy link
Author

결과는 모든 쓰레드가 하나의 쓰레드를 기다려서 받는 것을 확인시켜 준다.

Thread-1 request a value to the cache.
sleeping 1 times Thread-1
Thread-2 request a value to the cache.
Thread-3 request a value to the cache.
Thread-4 request a value to the cache.
Thread-5 request a value to the cache.
Thread-6 request a value to the cache.
sleeping 2 times Thread-1
Thread-7 request a value to the cache.
Thread-8 request a value to the cache.
Thread-9 request a value to the cache.
Thread-10 request a value to the cache.
sleeping 3 times Thread-1
sleeping 4 times Thread-1
sleeping 5 times Thread-1
sleeping 6 times Thread-1
sleeping 7 times Thread-1
sleeping 8 times Thread-1
sleeping 9 times Thread-1
sleeping 10 times Thread-1
Thread  Thread-2 got answer "I'm fine, thank you, and you?".
Thread  Thread-5 got answer "I'm fine, thank you, and you?".
Thread  Thread-7 got answer "I'm fine, thank you, and you?".
Thread  Thread-6 got answer "I'm fine, thank you, and you?".
Thread  Thread-4 got answer "I'm fine, thank you, and you?".
Thread  Thread-9 got answer "I'm fine, thank you, and you?".
Thread  Thread-3 got answer "I'm fine, thank you, and you?".
Thread  Thread-8 got answer "I'm fine, thank you, and you?".
Thread  Thread-1 got answer "I'm fine, thank you, and you?".
Thread  Thread-10 got answer "I'm fine, thank you, and you?".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment