Skip to content

Commit f9510f0

Browse files
committed
Add MiscUtility fields
- `RANDOM` - `AVAILABLE_PROCESSORS` - `CPU_SCHEDULER` - `IO_SCHEDULER`
1 parent eb2aa40 commit f9510f0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main/java/xyz/srnyx/javautilities/MiscUtility.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import java.util.Arrays;
66
import java.util.Optional;
7+
import java.util.Random;
78
import java.util.Set;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.ScheduledExecutorService;
811
import java.util.function.Supplier;
912
import java.util.stream.Collectors;
1013

@@ -13,6 +16,23 @@
1316
* General utility methods
1417
*/
1518
public class MiscUtility {
19+
/**
20+
* A {@link Random} instance
21+
*/
22+
@NotNull public static final Random RANDOM = new Random();
23+
/**
24+
* The number of available CPU processors
25+
*/
26+
public static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
27+
/**
28+
* A {@link ScheduledExecutorService} for CPU intensive tasks (heavy computation, such as mathematical calculations or data processing)
29+
*/
30+
@NotNull public static final ScheduledExecutorService CPU_SCHEDULER = Executors.newScheduledThreadPool(AVAILABLE_PROCESSORS);
31+
/**
32+
* A {@link ScheduledExecutorService} for IO intensive tasks (waiting for external resources, such as reading/writing files, making network requests, or querying a database)
33+
*/
34+
@NotNull public static final ScheduledExecutorService IO_SCHEDULER = Executors.newScheduledThreadPool(AVAILABLE_PROCESSORS * 2);
35+
1636
/**
1737
* If specific throwables are thrown by the {@link Supplier}, {@code null} is returned
1838
*

0 commit comments

Comments
 (0)