1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package cn.heartisai;
import cn.hutool.core.thread.NamedThreadFactory;
import java.util.*; import java.util.concurrent.*; import java.util.function.Predicate;
public class ParallelExecutor {
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() / 2, Runtime.getRuntime().availableProcessors() * 4, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(200), new NamedThreadFactory("parallel", false), (r, executor) -> r.run());
public static <T> TimeLimitedExecutor<T> newTimeLimitedExecutor(long limitedTime) { return new TimeLimitedExecutor<>(limitedTime); }
public static <T> TimeLimitedExecutor<T> newTimeLimitedExecutor(long limitedTime, long subtleTime) { return new TimeLimitedExecutor<>(limitedTime, subtleTime); }
public static class TimeLimitedExecutor<T> {
private final long limitedTime; private long subtleTime;
private final ExecutorCompletionService<T> completionService;
List<Callable<T>> callableList = new ArrayList<>();
public TimeLimitedExecutor(long limitedTime) { this(limitedTime, 20L); }
public TimeLimitedExecutor(long limitedTime, long subtleTime) { this.limitedTime = limitedTime; this.subtleTime = subtleTime; completionService = new ExecutorCompletionService<>(executor); }
public TimeLimitedExecutor<T> setSubtleTime(long subtleTime) { this.subtleTime = subtleTime; return this; }
public TimeLimitedExecutor<T> submit(Callable<T> callable) { callableList.add(callable); return this; }
public TimeLimitedExecutor<T> submit(Runnable runnable, T result) { callableList.add(() -> { runnable.run(); return result; }); return this; }
public List<T> execute() { return execute(null); }
public List<T> execute(Predicate<T> stopWhen) { if (callableList.isEmpty()) { return Collections.emptyList(); }
if (stopWhen == null) { stopWhen = (v) -> false; }
List<Future<T>> futureList = new ArrayList<>(); for (Callable<T> c : callableList) { futureList.add(completionService.submit(c)); }
List<T> list = new ArrayList<>(); Set<Future<T>> set = new HashSet<>(); final long startTime = System.currentTimeMillis(); for (int i = 0, len = callableList.size(); i < len; i++) { long pollTime = i == 0 ? limitedTime : limitedTime - (System.currentTimeMillis() - startTime); if (pollTime <= 5) { pollTime = subtleTime; } Future<T> future = null; try { future = completionService.poll(pollTime, TimeUnit.MILLISECONDS); } catch (Exception e) { System.out.println("获取future异常了"); e.printStackTrace(); } if (future == null) { System.out.println("超时出间范围了"); } else { try { T value = future.get(); set.add(future); list.add(value); if (stopWhen.test(value)) { break; } } catch (Exception e) { System.out.println("获取数据异常了"); e.printStackTrace(); } } }
if (set.size() < futureList.size()) { for (Future<T> f : futureList) { if (!set.contains(f) && !f.isCancelled()) { f.cancel(true); } } }
return list;
}
}
}
|