|
4 | 4 | import java.util.concurrent.Executors;
|
5 | 5 |
|
6 | 6 | /**
|
7 |
| - * @author ding.lid |
| 7 | + * @author Jerry Lee (oldratlee at gmail dot com) |
8 | 8 | */
|
9 | 9 | 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; |
14 | 16 |
|
15 |
| - @SuppressWarnings("InfiniteLoopStatement") |
16 | 17 | public static synchronized void makeLoad() {
|
17 |
| - if (isLoadMade) return; |
| 18 | + isLoadMade = true; |
18 | 19 |
|
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; |
23 | 25 | sleep(1);
|
24 | 26 | }
|
25 | 27 | }
|
26 | 28 | });
|
27 | 29 | }
|
28 | 30 | }
|
29 | 31 |
|
30 |
| - public static void sleep(long l) { |
| 32 | + public static void stopLoad() { |
| 33 | + isLoadMade = false; |
| 34 | + } |
| 35 | + |
| 36 | + public static void sleep(long millis) { |
31 | 37 | try {
|
32 |
| - Thread.sleep(l); |
| 38 | + Thread.sleep(millis); |
33 | 39 | } catch (InterruptedException ignored) {
|
34 | 40 | }
|
35 | 41 | }
|
|
0 commit comments