前情摘要:工作中使用 ExecuteService.execute(Runnable runnable)方法 進行多線程的數據插入,出現部分未執行,數據沒有進入數據庫。后改為Future future = ExecuteService.submit(Callable task)方法后未出現前面的BUG。
源代碼示例如下:
package com.wangdada.project; import org.junit.Test; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.*; /** * ExecutorService測試類 * * @author 硬核技術 https://www.cnblogs.com/yhjs/ * date 2020-10-20 22:42 */ public class ThreadTest { final String column1 = "id"; final String column2 = "name"; @Test public void runThread() { // 將map當作一個entity List<Map<String, String>> dataList = new LinkedList<>(); prepareData(dataList); System.out.println("開始執行..."); // executeRunnable(dataList); executeFuture(dataList); System.out.println("結束執行..."); } private void executeRunnable(List<Map<String, String>> dataList) { ExecutorService executorService = Executors.newFixedThreadPool(10); dataList.forEach(map -> { Runnable runnable = () -> { try { // 睡眠當作執行了業務代碼 Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("threadId: %s, id: %s, name: %s%n", Thread.currentThread().getId(), map.get(column1), map.get(column2)); }; executorService.execute(runnable); }); } private void executeFuture(List<Map<String, String>> dataList) { ExecutorService executorService = Executors.newFixedThreadPool(10); List<Future<String>> futureList = new LinkedList<>(); dataList.forEach(map -> { Callable<String> callable = () -> { try { // 睡眠當作執行了業務代碼 Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("threadId: %s, id: %s, name: %s%n", Thread.currentThread().getId(), map.get(column1), map.get(column2)); return "Success"; }; Future<String> future = executorService.submit(callable); futureList.add(future); }); futureList.forEach(future -> { try { future.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }); } private void prepareData(List<Map<String, String>> dataList) { int size = 10; for (int i = 0; i < size; i++) { Map<String, String> data = new HashMap<>(); data.put(column1, i + ""); data.put(column2, column2 + i); dataList.add(data); } } }
運行executeRunnable方法得到如下結果:
運行executeFuture方法得到如下結果:
結論:使用executorService.execute(Runnable)異步執行未執行完成就結束了。相反第二種方法執行結果正確。