11package dev .felnull .fnjl .util ;
22
3+ import org .jetbrains .annotations .NotNull ;
4+ import org .jetbrains .annotations .Range ;
5+
36import java .util .*;
47import java .util .concurrent .CompletableFuture ;
8+ import java .util .concurrent .Executor ;
9+ import java .util .concurrent .TimeUnit ;
510import java .util .concurrent .atomic .AtomicInteger ;
611
712public class FNRuntimeUtil {
813 private static final Map <Runnable , Long > lastTimes = new HashMap <>();
914 private static final List <Timer > timers = new ArrayList <>();
1015
16+ /**
17+ * 1日ごとに決まった時間に実行
18+ *
19+ * @param timer タイマー
20+ * @param executor エクスキューター
21+ * @param hours 時
22+ * @param minutes 分
23+ * @param runnable 実行
24+ */
25+ public static void loopDayRunner (@ NotNull Timer timer , @ NotNull Executor executor , @ Range (from = 0 , to = 23 ) int hours , @ Range (from = 0 , to = 59 ) int minutes , @ NotNull Runnable runnable ) {
26+ Calendar calendar = Calendar .getInstance ();
27+ calendar .setTime (new Date ());
28+
29+ if (calendar .get (Calendar .HOUR_OF_DAY ) > hours || (calendar .get (Calendar .HOUR_OF_DAY ) == hours && calendar .get (Calendar .MINUTE ) >= minutes ))
30+ calendar .add (Calendar .DATE , 1 );
31+
32+ calendar .set (Calendar .HOUR_OF_DAY , hours );
33+ calendar .set (Calendar .MINUTE , minutes );
34+
35+ loopDayRunner_ (calendar , timer , executor , runnable );
36+ }
37+
38+ private static void loopDayRunner_ (Calendar calendar , Timer timer , Executor executor , Runnable runnable ) {
39+ dayRunner (timer , executor , calendar .getTime (), () -> {
40+ calendar .add (Calendar .DATE , 1 );
41+ loopDayRunner_ (calendar , timer , executor , runnable );
42+ runnable .run ();
43+ });
44+ }
45+
46+ /**
47+ * 指定の日付時刻に実行
48+ *
49+ * @param timer タイマー
50+ * @param executor エクスキューター
51+ * @param date 日付時刻
52+ * @param runnable 実行
53+ */
54+ public static void dayRunner (@ NotNull Timer timer , @ NotNull Executor executor , @ NotNull Date date , @ NotNull Runnable runnable ) {
55+ TimerTask task = new TimerTask () {
56+ @ Override
57+ public void run () {
58+ CompletableFuture .runAsync (runnable , executor );
59+ }
60+ };
61+ timer .schedule (task , date );
62+ }
63+
64+ /**
65+ * 指定時間ごとに繰り返して実行
66+ *
67+ * @param timer タイマー
68+ * @param executor エクスキューター
69+ * @param delayTimeUnit 時間単位
70+ * @param delayDuration 間隔
71+ * @param runnable 実行
72+ */
73+ public static void loopRunner (@ NotNull Timer timer , @ NotNull Executor executor , @ NotNull TimeUnit delayTimeUnit , long delayDuration , @ NotNull Runnable runnable ) {
74+ loopRunner (timer , executor , false , delayTimeUnit , delayDuration , runnable );
75+ }
76+
77+ /**
78+ * 指定時間ごとに繰り返して実行
79+ *
80+ * @param timer タイマー
81+ * @param executor エクスキューター
82+ * @param startDelay 最初に間隔を開けるかどうか
83+ * @param delayTimeUnit 時間単位
84+ * @param delayDuration 間隔
85+ * @param runnable 実行
86+ */
87+ public static void loopRunner (@ NotNull Timer timer , @ NotNull Executor executor , boolean startDelay , @ NotNull TimeUnit delayTimeUnit , long delayDuration , @ NotNull Runnable runnable ) {
88+ TimerTask task = new TimerTask () {
89+ @ Override
90+ public void run () {
91+ CompletableFuture .runAsync (runnable , executor );
92+ }
93+ };
94+ long time = delayTimeUnit .toMillis (delayDuration );
95+ timer .scheduleAtFixedRate (task , startDelay ? time : 0 , time );
96+ }
97+
98+ /**
99+ * 指定時間後に遅れて実行
100+ *
101+ * @param timer タイマー
102+ * @param executor エクスキューター
103+ * @param delayTimeUnit 時間単位
104+ * @param delayDuration 遅れ
105+ * @param runnable 実行
106+ */
107+ public static void delayRunner (@ NotNull Timer timer , @ NotNull Executor executor , @ NotNull TimeUnit delayTimeUnit , long delayDuration , @ NotNull Runnable runnable ) {
108+ TimerTask task = new TimerTask () {
109+ @ Override
110+ public void run () {
111+ CompletableFuture .runAsync (runnable , executor );
112+ }
113+ };
114+ timer .schedule (task , delayTimeUnit .toMillis (delayDuration ));
115+ }
116+
11117 public static void oneDayClockTimer (int hours , int minutes , Runnable runnable ) {
12118 oneDayClockTimer (hours , minutes , runnable , false );
13119 }
@@ -41,6 +147,7 @@ public void run() {
41147 * @param runnables 実行
42148 * @return CompletableFuture
43149 */
150+ @ Deprecated
44151 public static CompletableFuture <Void > multipleRun (Runnable runnable , Runnable ... runnables ) {
45152 return multipleRun (null , -1 , runnable , runnables );
46153 }
@@ -53,6 +160,7 @@ public static CompletableFuture<Void> multipleRun(Runnable runnable, Runnable...
53160 * @param runnables 実行
54161 * @return CompletableFuture
55162 */
163+ @ Deprecated
56164 public static CompletableFuture <Void > multipleRun (String threadName , Runnable runnable , Runnable ... runnables ) {
57165 return multipleRun (threadName , -1 , runnable , runnables );
58166 }
@@ -63,6 +171,7 @@ public static CompletableFuture<Void> multipleRun(String threadName, Runnable ru
63171 * @param runnables 実行
64172 * @return CompletableFuture
65173 */
174+ @ Deprecated
66175 public static CompletableFuture <Void > multipleRun (int max , Runnable runnable , Runnable ... runnables ) {
67176 return multipleRun (null , max , runnable , runnables );
68177 }
@@ -76,6 +185,7 @@ public static CompletableFuture<Void> multipleRun(int max, Runnable runnable, Ru
76185 * @param runnables 実行
77186 * @return CompletableFuture
78187 */
188+ @ Deprecated
79189 public static CompletableFuture <Void > multipleRun (String threadName , int max , Runnable runnable , Runnable ... runnables ) {
80190 if (max <= 0 )
81191 max = 1 + runnables .length ;
0 commit comments