參考:https://blog.csdn.net/u011001084/article/details/104037805
結論:參考文章的性能測出的結果 與我實際測試出的結果正好相反, 所以開發還是要實測為准。
添加依賴:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
基本用法:
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException { Cache<String,String> cache1 =CacheBuilder.newBuilder() .expireAfterWrite(3,TimeUnit.SECONDS) // 若緩存項在給定的時間內沒有被寫訪問,則回收(延時回收)。 .expireAfterAccess(3,TimeUnit.SECONDS) // 若緩存項在給定的實際內沒有被讀訪問,則回收(延時回收)。 .weakValues() // 弱引用存儲值,可垃圾回收 .weakKeys() // 弱引用存儲鍵,可垃圾回收 .concurrencyLevel(8) // 並發級別,指同時支持寫緩存的線程數 .initialCapacity(10) // 初始化容量 .maximumSize(2000) // 最大存儲上限,超過按照LRU算法移除緩存項 .recordStats() // 統計緩存的命中率 .removalListener(new MyListener()) // 緩存的移除監聽(監聽到的是新值) .build(); cache1.put("myKey","myValue"); System.out.println("獲取值:" + cache1.getIfPresent("myKey")); cache1.invalidate("myKey"); System.out.println("獲取我的key:" + cache1.get("myKey",()->"操作數據庫")); Thread.sleep(5000); System.out.println("再次獲取:" + cache1.get("myKey",()->"再次操作數據庫")); cache1.put("myKey11","myValue11"); // 自然的過期,並不會觸發監聽 Thread.sleep(5000); System.out.println(cache1.getIfPresent("myKey11")); System.in.read(); } private static class MyListener implements RemovalListener<String,String>{ @Override public void onRemoval(RemovalNotification<String, String> notification) { System.out.println("修改的key:" + notification.getKey()); System.out.println("修改的值:" + notification.getValue()); System.out.println("修改的原因:" + notification.getCause()); } } 以下是線程池的性能測試: // Future的性能測試 public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); long startTime = System.currentTimeMillis(); GuavaMain guavaMain = new GuavaMain(); List<Future<String>> futureList = Arrays.asList(executor.submit(() -> guavaMain.test1()), executor.submit(() -> guavaMain.test2()), executor.submit(() -> guavaMain.test3()), executor.submit(() -> guavaMain.test4())); List<String> collect = futureList.stream().map(future -> { try { return future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return null; }).collect(Collectors.toList()); System.out.println("耗時:" + (System.currentTimeMillis() - startTime)); } // CompletableFuture性能測試 public static void main4(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); long startTime = System.currentTimeMillis(); GuavaMain guavaMain = new GuavaMain(); List<CompletableFuture<String>> futures = new ArrayList<>(4); futures.add(CompletableFuture.supplyAsync(() -> guavaMain.test1(),executor)); futures.add(CompletableFuture.supplyAsync(() -> guavaMain.test2(),executor)); futures.add(CompletableFuture.supplyAsync(() -> guavaMain.test3(),executor)); futures.add(CompletableFuture.supplyAsync(() -> guavaMain.test4(),executor)); CompletableFuture<Void> allFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[4])); CompletableFuture<List<String>> listCompletableFuture = allFuture.thenApplyAsync(value -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()), executor); List<String> join = listCompletableFuture.join(); System.out.println("耗時:" + (System.currentTimeMillis() - startTime)); } // guava性能測試 public static void main5(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newCachedThreadPool(); ListeningExecutorService guavaExecutor = MoreExecutors.listeningDecorator(executor); long startTime = System.currentTimeMillis(); GuavaMain guavaMain = new GuavaMain(); ListenableFuture<String> lf1 = guavaExecutor.submit(() -> guavaMain.test1()); ListenableFuture<String> lf2 = guavaExecutor.submit(() -> guavaMain.test2()); ListenableFuture<String> lf3 = guavaExecutor.submit(() -> guavaMain.test3()); ListenableFuture<String> lf4 = guavaExecutor.submit(() -> guavaMain.test4()); ListenableFuture<List<String>> listListenableFuture = Futures.allAsList(lf1, lf2, lf3, lf4); List<String> strings = listListenableFuture.get(); System.out.println("耗時:" + (System.currentTimeMillis() - startTime)); } private String test1(){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "test1"; } private String test2(){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "test2"; } private String test3(){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "test3"; } private String test4(){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "test4"; }