Skip to content

Commit 6876371

Browse files
committed
add runtime util
1 parent fb9f1b9 commit 6876371

File tree

5 files changed

+180
-37
lines changed

5 files changed

+180
-37
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
group 'dev.felnull'
88
archivesBaseName = "felnull-java-library"
9-
version '1.20'
9+
version '1.21'
1010

1111
repositories {
1212
mavenCentral()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.felnull.fnjl;
22

33
public class BuildIn {
4-
protected static final String VERSION = "1.20";
4+
protected static final String VERSION = "1.21";
55

66
protected static final int NATIVE_LIB_VERSION = 2;
77
}

src/main/java/dev/felnull/fnjl/util/FNClockUtil.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
package dev.felnull;
22

3-
import dev.felnull.fnjl.math.FNVec2i;
4-
53
public class TestMain {
64
public static void main(String[] args) throws Exception {
7-
System.out.println(new FNVec2i(0, 0).distance(new FNVec2i(1, 1)));
5+
/* long time = System.currentTimeMillis();
6+
FNRuntimeUtil.multipleRun("Ikisugi", 2, () -> {
7+
try {
8+
Thread.sleep(1000);
9+
} catch (InterruptedException e) {
10+
e.printStackTrace();
11+
}
12+
System.out.println("test1" + " " + Thread.currentThread().getName());
13+
}, () -> {
14+
try {
15+
Thread.sleep(1000);
16+
} catch (InterruptedException e) {
17+
e.printStackTrace();
18+
}
19+
System.out.println("test2" + " " + Thread.currentThread().getName());
20+
}, () -> {
21+
try {
22+
Thread.sleep(1000);
23+
} catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
System.out.println("test3" + " " + Thread.currentThread().getName());
27+
}, () -> {
28+
try {
29+
Thread.sleep(1000);
30+
} catch (InterruptedException e) {
31+
e.printStackTrace();
32+
}
33+
System.out.println("test4" + " " + Thread.currentThread().getName());
34+
}, () -> {
35+
try {
36+
Thread.sleep(1000);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
}
40+
System.out.println("test4" + " " + Thread.currentThread().getName());
41+
}, () -> {
42+
try {
43+
Thread.sleep(1000);
44+
} catch (InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
System.out.println("test4" + " " + Thread.currentThread().getName());
48+
}).get();
49+
System.out.println(System.currentTimeMillis() - time);*/
850
}
951
}

0 commit comments

Comments
 (0)