Javaマルチスレッドは複数のタスクを同時に処理し、結果を取得します.
5108 ワード
使用・発注中のCallableおよびFuture類処理.
あまり話さないで、直接コードをつけます:
0
テストクラス:
実行結果:
あまり話さないで、直接コードをつけます:
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> callables = new ArrayList<>();
long start = System.currentTimeMillis();
log.info("start----------------------" + start);
for (int i = 0; i < 10; i++) {
callables.add(new Mycallable(i));
}
// List res = addTask(callables);
List> futureList;
try {
futureList = executorService.invokeAll(callables);
// for (Future 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