aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/mayaqq/ygasi/util/Multithreading.java
blob: 89914d1a8fdf5afc9dcf5f4033c5404252027cec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dev.mayaqq.ygasi.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.*;

/**
 * Allows for easy multithreading.
 * <p>
 * Taken from Seraph by Scherso under LGPL-2.1
 * <a href="https://github.com/Scherso/Seraph/blob/master/LICENSE">https://github.com/Scherso/Seraph/blob/master/LICENSE</a>
 * </p>
 */
public class Multithreading {
    private static final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("OneConfig-%d").build());
    private static final ScheduledExecutorService runnableExecutor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() + 1);

    /**
     * Runs the runnable asynchronously.
     *
     * @param runnable The runnable to run.
     * @see Multithreading#submit(Runnable)
     */
    public static void runAsync(Runnable runnable) {
        submit(runnable);
    }

    /**
     * Runs the provided runnables asynchronously.
     *
     * @param runnables The runnables to run.
     * @see Multithreading#runAsync(Runnable)
     */
    public static void runAsync(Runnable... runnables) {
        for (Runnable runnable : runnables) {
            runAsync(runnable);
        }
    }

    /**
     * Submits the Runnable to the executor, making it run asynchronously.
     *
     * @param runnable The runnable to run.
     * @return The future representing the submitted runnable.
     * @see ExecutorService#submit(Runnable)
     */
    public static Future<?> submit(Runnable runnable) {
        return executorService.submit(runnable);
    }

    /**
     * Schedules the runnable to run asynchronously after the specified delay.
     *
     * @param runnable The runnable to run.
     * @param delay    The delay before the runnable is run.
     * @param timeUnit The {@link TimeUnit} of the delay.
     * @see Multithreading#submitScheduled(Runnable, long, TimeUnit)
     */
    public static void schedule(Runnable runnable, long delay, TimeUnit timeUnit) {
        submitScheduled(runnable, delay, timeUnit);
    }

    /**
     * Submits the Runnable to the executor after a delay, making it run asynchronously.
     *
     * @param runnable The runnable to run.
     * @param delay    The delay before the runnable is run.
     * @param timeUnit The {@link TimeUnit} of the delay.
     * @return The future representing the submitted runnable.
     * @see ScheduledExecutorService#schedule(Runnable, long, TimeUnit)
     */
    public static ScheduledFuture<?> submitScheduled(Runnable runnable, long delay, TimeUnit timeUnit) {
        return runnableExecutor.schedule(runnable, delay, timeUnit);
    }
}