|
| 1 | +package dev.felnull.fnjl.util; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | +import java.util.concurrent.atomic.AtomicInteger; |
| 6 | + |
| 7 | +public class FNRuntimeUtil { |
| 8 | + private static final Map<Runnable, Long> lastTimes = new HashMap<>(); |
| 9 | + |
| 10 | + public static void oneDayClockTimer(int hours, int minutes, Runnable runnable) { |
| 11 | + oneDayClockTimer(hours, minutes, runnable, false); |
| 12 | + } |
| 13 | + |
| 14 | + public static void oneDayClockTimer(int hours, int minutes, Runnable runnable, boolean daemon) { |
| 15 | + Timer timer = new Timer(daemon); |
| 16 | + TimerTask task = new TimerTask() { |
| 17 | + @Override |
| 18 | + public void run() { |
| 19 | + long lastTime = 0; |
| 20 | + if (lastTimes.containsKey(runnable)) |
| 21 | + lastTime = lastTimes.get(runnable); |
| 22 | + |
| 23 | + if (System.currentTimeMillis() - lastTime > 60 * 60 * 1000) { |
| 24 | + Date date = new Date(); |
| 25 | + if (date.getHours() == hours && date.getMinutes() == minutes) { |
| 26 | + runnable.run(); |
| 27 | + lastTimes.put(runnable, System.currentTimeMillis()); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + timer.schedule(task, 0, 1000); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * マルチ非同期実行 |
| 37 | + * |
| 38 | + * @param runnable 実行 |
| 39 | + * @param runnables 実行 |
| 40 | + * @return CompletableFuture |
| 41 | + */ |
| 42 | + public static CompletableFuture<Void> multipleRun(Runnable runnable, Runnable... runnables) { |
| 43 | + return multipleRun(null, -1, runnable, runnables); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * マルチ非同期実行 |
| 48 | + * |
| 49 | + * @param threadName スレッド名 |
| 50 | + * @param runnable 実行 |
| 51 | + * @param runnables 実行 |
| 52 | + * @return CompletableFuture |
| 53 | + */ |
| 54 | + public static CompletableFuture<Void> multipleRun(String threadName, Runnable runnable, Runnable... runnables) { |
| 55 | + return multipleRun(threadName, -1, runnable, runnables); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param max 最大同時thread |
| 60 | + * @param runnable 実行 |
| 61 | + * @param runnables 実行 |
| 62 | + * @return CompletableFuture |
| 63 | + */ |
| 64 | + public static CompletableFuture<Void> multipleRun(int max, Runnable runnable, Runnable... runnables) { |
| 65 | + return multipleRun(null, max, runnable, runnables); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * マルチ非同期実行 |
| 70 | + * |
| 71 | + * @param threadName スレッド名 |
| 72 | + * @param max 最大同時thread |
| 73 | + * @param runnable 実行 |
| 74 | + * @param runnables 実行 |
| 75 | + * @return CompletableFuture |
| 76 | + */ |
| 77 | + public static CompletableFuture<Void> multipleRun(String threadName, int max, Runnable runnable, Runnable... runnables) { |
| 78 | + if (max <= 0) |
| 79 | + max = 1 + runnables.length; |
| 80 | + int mx = max; |
| 81 | + return CompletableFuture.runAsync(() -> { |
| 82 | + Object obj = new Object(); |
| 83 | + AtomicInteger fin = new AtomicInteger(); |
| 84 | + AtomicInteger maxFin = new AtomicInteger(); |
| 85 | + List<Thread> runners = new ArrayList<>(); |
| 86 | + runners.add(new Thread(() -> { |
| 87 | + runnable.run(); |
| 88 | + synchronized (obj) { |
| 89 | + fin.getAndIncrement(); |
| 90 | + if (fin.get() >= maxFin.get()) |
| 91 | + obj.notifyAll(); |
| 92 | + } |
| 93 | + })); |
| 94 | + for (Runnable run : runnables) { |
| 95 | + runners.add(new Thread(() -> { |
| 96 | + run.run(); |
| 97 | + synchronized (obj) { |
| 98 | + fin.getAndIncrement(); |
| 99 | + if (fin.get() >= maxFin.get()) |
| 100 | + obj.notifyAll(); |
| 101 | + } |
| 102 | + })); |
| 103 | + } |
| 104 | + if (threadName != null) { |
| 105 | + for (int i = 0; i < runners.size(); i++) { |
| 106 | + runners.get(i).setName(threadName + "-" + i); |
| 107 | + } |
| 108 | + } |
| 109 | + do { |
| 110 | + List<Thread> rms = new ArrayList<>(); |
| 111 | + int ct = 0; |
| 112 | + for (Thread runner : runners) { |
| 113 | + rms.add(runner); |
| 114 | + ct++; |
| 115 | + if (ct >= mx) |
| 116 | + break; |
| 117 | + } |
| 118 | + maxFin.set(ct); |
| 119 | + rms.forEach(Thread::start); |
| 120 | + runners.removeAll(rms); |
| 121 | + synchronized (obj) { |
| 122 | + try { |
| 123 | + if (fin.get() < maxFin.get()) |
| 124 | + obj.wait(); |
| 125 | + } catch (InterruptedException ignored) { |
| 126 | + } |
| 127 | + } |
| 128 | + fin.set(0); |
| 129 | + } while (!runners.isEmpty()); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments