ExecutorService.execute(Runnable runnable)方法與ExecutorService.submit(Callable task)運行效果差別


前情摘要:工作中使用 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)異步執行未執行完成就結束了。相反第二種方法執行結果正確。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM