|
| 1 | +package com.zzang.chongdae.global.helper; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.CountDownLatch; |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.Future; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +public class ConcurrencyExecutor { |
| 13 | + |
| 14 | + private static ConcurrencyExecutor INSTANCE; |
| 15 | + |
| 16 | + public static ConcurrencyExecutor getInstance() { |
| 17 | + if (INSTANCE == null) { |
| 18 | + INSTANCE = new ConcurrencyExecutor(); |
| 19 | + } |
| 20 | + return INSTANCE; |
| 21 | + } |
| 22 | + |
| 23 | + public void executeWithoutResult(Runnable... tasks) throws InterruptedException { |
| 24 | + int executeCount = tasks.length; |
| 25 | + ExecutorService executorService = Executors.newFixedThreadPool(executeCount); |
| 26 | + CountDownLatch countDownLatch = new CountDownLatch(executeCount); |
| 27 | + |
| 28 | + for (Runnable task : tasks) { |
| 29 | + executorService.submit(() -> { |
| 30 | + try { |
| 31 | + task.run(); |
| 32 | + } finally { |
| 33 | + countDownLatch.countDown(); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + countDownLatch.await(); |
| 39 | + executorService.shutdown(); |
| 40 | + } |
| 41 | + |
| 42 | + public void executeWithoutResult(int repeatCount, Runnable task) throws InterruptedException { |
| 43 | + ExecutorService executorService = Executors.newFixedThreadPool(repeatCount); |
| 44 | + CountDownLatch countDownLatch = new CountDownLatch(repeatCount); |
| 45 | + |
| 46 | + for (int i = 0; i < repeatCount; i++) { |
| 47 | + executorService.submit(() -> { |
| 48 | + try { |
| 49 | + task.run(); |
| 50 | + } finally { |
| 51 | + countDownLatch.countDown(); |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + countDownLatch.await(); |
| 57 | + executorService.shutdown(); |
| 58 | + } |
| 59 | + |
| 60 | + public final <T> List<T> execute(Supplier<T>... tasks) throws InterruptedException { |
| 61 | + int executeCount = tasks.length; |
| 62 | + ExecutorService executorService = Executors.newFixedThreadPool(executeCount); |
| 63 | + CountDownLatch countDownLatch = new CountDownLatch(executeCount); |
| 64 | + |
| 65 | + List<Future<T>> result = new ArrayList<>(); |
| 66 | + for (Supplier<T> task : tasks) { |
| 67 | + Future<T> future = executorService.submit(() -> { |
| 68 | + try { |
| 69 | + return task.get(); |
| 70 | + } finally { |
| 71 | + countDownLatch.countDown(); |
| 72 | + } |
| 73 | + }); |
| 74 | + result.add(future); |
| 75 | + } |
| 76 | + |
| 77 | + countDownLatch.await(); |
| 78 | + executorService.shutdown(); |
| 79 | + |
| 80 | + return result.stream() |
| 81 | + .map(this::getWithoutException) |
| 82 | + .toList(); |
| 83 | + } |
| 84 | + |
| 85 | + public <T> List<T> execute(int repeatCount, Supplier<T> task) throws InterruptedException { |
| 86 | + ExecutorService executorService = Executors.newFixedThreadPool(repeatCount); |
| 87 | + CountDownLatch countDownLatch = new CountDownLatch(repeatCount); |
| 88 | + |
| 89 | + List<Future<T>> result = new ArrayList<>(); |
| 90 | + for (int i = 0; i < repeatCount; i++) { |
| 91 | + Future<T> future = executorService.submit(() -> { |
| 92 | + try { |
| 93 | + return task.get(); |
| 94 | + } finally { |
| 95 | + countDownLatch.countDown(); |
| 96 | + } |
| 97 | + }); |
| 98 | + result.add(future); |
| 99 | + } |
| 100 | + |
| 101 | + countDownLatch.await(); |
| 102 | + executorService.shutdown(); |
| 103 | + |
| 104 | + return result.stream() |
| 105 | + .map(this::getWithoutException) |
| 106 | + .toList(); |
| 107 | + } |
| 108 | + |
| 109 | + private <T> T getWithoutException(Future<T> future) { |
| 110 | + try { |
| 111 | + return future.get(); |
| 112 | + } catch (InterruptedException | ExecutionException e) { |
| 113 | + throw new RuntimeException("동시 작업 내부 예외 발생: ", e); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments