代码
LoadingCache<String, String> loadingCache = CacheBuilder.newBuilder() .refreshAfterWrite(2, TimeUnit.SECONDS).maximumSize(5) .build(CacheLoader.asyncReloading(new CacheLoader<String, String>() { @Override public String load(String key) throws Exception { System.out.println(Thread.currentThread().getName() + " 加载数据开始---"); Thread.sleep(1000); Random random = new Random(); System.out.println(Thread.currentThread().getName() + " 加载数据结束----"); return "value:" + random.nextInt(10000); } }, ForkJoinPool.commonPool())); while (true){ // String s = cache.get(1); String s = loadingCache.get("1"); System.out.println(s); Thread.sleep(100); }