Skip to content

Commit 26bbd64

Browse files
committed
refactor LoadMaker
1 parent 09a12f9 commit 26bbd64

File tree

1 file changed

+19
-13
lines changed
  • src/main/java/com/oldratlee/fucking/concurrency/util

1 file changed

+19
-13
lines changed

src/main/java/com/oldratlee/fucking/concurrency/util/Utils.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@
44
import java.util.concurrent.Executors;
55

66
/**
7-
* @author ding.lid
7+
* @author Jerry Lee (oldratlee at gmail dot com)
88
*/
99
public class Utils {
10-
static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
11-
static final int THREAD_COUNT = CPU_COUNT * 2;
12-
static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
13-
static volatile boolean isLoadMade = false;
10+
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
11+
private static final int THREAD_COUNT = CPU_COUNT * 2;
12+
13+
private static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);
14+
15+
private static volatile boolean isLoadMade = false;
1416

15-
@SuppressWarnings("InfiniteLoopStatement")
1617
public static synchronized void makeLoad() {
17-
if (isLoadMade) return;
18+
isLoadMade = true;
1819

19-
for (int i = 0; i < THREAD_COUNT; ++i) {
20-
executorService.submit((Runnable) () -> {
21-
for (int i1 = 1; ; ++i1) {
22-
if (i1 % 1000000 == 0) {
20+
for (int taskCount = 0; taskCount < THREAD_COUNT; ++taskCount) {
21+
executorService.submit(() -> {
22+
for (int i = 1; ; ++i) {
23+
if (i % 1_000_000 == 0) {
24+
if (!isLoadMade) return;
2325
sleep(1);
2426
}
2527
}
2628
});
2729
}
2830
}
2931

30-
public static void sleep(long l) {
32+
public static void stopLoad() {
33+
isLoadMade = false;
34+
}
35+
36+
public static void sleep(long millis) {
3137
try {
32-
Thread.sleep(l);
38+
Thread.sleep(millis);
3339
} catch (InterruptedException ignored) {
3440
}
3541
}

0 commit comments

Comments
 (0)