Java多線程併發處理多個任務並獲取結果

使用併發包中的Callable及Future類處理。

話不多少,直接上代碼:


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;

/**
 * @Author: lp
 * @Date: 2019-
 */
@Slf4j
public class Mycallable implements Callable {

    Integer finalI;

    public Mycallable(Integer finalI) {
        this.finalI = finalI;
    }

    @Override
    public String call() throws Exception {

        long millis = new Double(Math.random() * 10000).longValue();
        log.info(finalI + ", " + millis);
        Thread.sleep(millis);
        return finalI + "";
    }
}

0

 

測試類:


import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 *
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"jasypt.encryptor.password=wN5"})
public class DemoThreadPoolTest {


    private static ExecutorService executorService = new ThreadPoolExecutor(10, 500,
            1, TimeUnit.SECONDS, new LinkedBlockingDeque());


    @Test
    public void test() {

        List<Callable<String>> callables = new ArrayList<>();
        long start = System.currentTimeMillis();
        log.info("start----------------------" + start);
        for (int i = 0; i < 10; i++) {
            callables.add(new Mycallable(i));
        }
//        List<String> res = addTask(callables);

        List<Future<String>> futureList;
        try {
            futureList = executorService.invokeAll(callables);
//            for (Future<String> future : futureList) {
//                log.info("resp: " + future.get());
//            }

            for (int i = 0; i < futureList.size(); i++) {
                log.info("resp: " + futureList.get(i).get());
            }

            long end = System.currentTimeMillis();
            long timeLong = end - start;
            log.info("result**********************" + timeLong);

        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

    }


}

 

 

執行結果:

2019-12-03 19:52:28.255  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 37 : start----------------------1575373948255
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-2] om.cmcc.hy.portray.api.thread.Mycallable 24 : 1, 9911
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-3] om.cmcc.hy.portray.api.thread.Mycallable 24 : 2, 9519
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-5] om.cmcc.hy.portray.api.thread.Mycallable 24 : 4, 7645
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-4] om.cmcc.hy.portray.api.thread.Mycallable 24 : 3, 5866
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-1] om.cmcc.hy.portray.api.thread.Mycallable 24 : 0, 8550
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-6] om.cmcc.hy.portray.api.thread.Mycallable 24 : 5, 500
2019-12-03 19:52:28.257  INFO 260784 [pool-2-thread-7] om.cmcc.hy.portray.api.thread.Mycallable 24 : 6, 6779
2019-12-03 19:52:28.258  INFO 260784 [ool-2-thread-10] om.cmcc.hy.portray.api.thread.Mycallable 24 : 9, 2409
2019-12-03 19:52:28.258  INFO 260784 [pool-2-thread-9] om.cmcc.hy.portray.api.thread.Mycallable 24 : 8, 1666
2019-12-03 19:52:28.259  INFO 260784 [pool-2-thread-8] om.cmcc.hy.portray.api.thread.Mycallable 24 : 7, 4171
2019-12-03 19:52:38.168  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 0
2019-12-03 19:52:38.168  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 1
2019-12-03 19:52:38.168  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 2
2019-12-03 19:52:38.168  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 3
2019-12-03 19:52:38.168  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 4
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 5
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 6
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 7
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 8
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 51 : resp: 9
2019-12-03 19:52:38.169  INFO 260784 [           main] hy.portray.api.thread.DemoThreadPoolTest 56 : result**********************9914

 

發佈了64 篇原創文章 · 獲贊 45 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章