1
1
package dev .felnull .fnjl .util ;
2
2
3
+ import org .jetbrains .annotations .NotNull ;
4
+ import org .jetbrains .annotations .Range ;
5
+
3
6
import java .util .*;
4
7
import java .util .concurrent .CompletableFuture ;
8
+ import java .util .concurrent .Executor ;
9
+ import java .util .concurrent .TimeUnit ;
5
10
import java .util .concurrent .atomic .AtomicInteger ;
6
11
7
12
public class FNRuntimeUtil {
8
13
private static final Map <Runnable , Long > lastTimes = new HashMap <>();
9
14
private static final List <Timer > timers = new ArrayList <>();
10
15
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
+
11
117
public static void oneDayClockTimer (int hours , int minutes , Runnable runnable ) {
12
118
oneDayClockTimer (hours , minutes , runnable , false );
13
119
}
@@ -41,6 +147,7 @@ public void run() {
41
147
* @param runnables 実行
42
148
* @return CompletableFuture
43
149
*/
150
+ @ Deprecated
44
151
public static CompletableFuture <Void > multipleRun (Runnable runnable , Runnable ... runnables ) {
45
152
return multipleRun (null , -1 , runnable , runnables );
46
153
}
@@ -53,6 +160,7 @@ public static CompletableFuture<Void> multipleRun(Runnable runnable, Runnable...
53
160
* @param runnables 実行
54
161
* @return CompletableFuture
55
162
*/
163
+ @ Deprecated
56
164
public static CompletableFuture <Void > multipleRun (String threadName , Runnable runnable , Runnable ... runnables ) {
57
165
return multipleRun (threadName , -1 , runnable , runnables );
58
166
}
@@ -63,6 +171,7 @@ public static CompletableFuture<Void> multipleRun(String threadName, Runnable ru
63
171
* @param runnables 実行
64
172
* @return CompletableFuture
65
173
*/
174
+ @ Deprecated
66
175
public static CompletableFuture <Void > multipleRun (int max , Runnable runnable , Runnable ... runnables ) {
67
176
return multipleRun (null , max , runnable , runnables );
68
177
}
@@ -76,6 +185,7 @@ public static CompletableFuture<Void> multipleRun(int max, Runnable runnable, Ru
76
185
* @param runnables 実行
77
186
* @return CompletableFuture
78
187
*/
188
+ @ Deprecated
79
189
public static CompletableFuture <Void > multipleRun (String threadName , int max , Runnable runnable , Runnable ... runnables ) {
80
190
if (max <= 0 )
81
191
max = 1 + runnables .length ;
0 commit comments