Threading utils. @author Matt
| 16 | * @author Matt |
| 17 | */ |
| 18 | public class ThreadUtil { |
| 19 | private static final ScheduledExecutorService scheduledService = |
| 20 | Executors.newScheduledThreadPool(threadCount(), |
| 21 | new ThreadFactoryBuilder() |
| 22 | .setNameFormat("Recaf Scheduler Thread #%d") |
| 23 | .setDaemon(true).build()); |
| 24 | private static final ExecutorService service = Executors.newWorkStealingPool(threadCount()); |
| 25 | |
| 26 | /** |
| 27 | * @param action |
| 28 | * Runnable to start in new thread. |
| 29 | * |
| 30 | * @return Thread future. |
| 31 | */ |
| 32 | public static Future<?> run(Runnable action) { |
| 33 | return service.submit(action); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param action |
| 38 | * Task to start in new thread. |
| 39 | * @param <T> |
| 40 | * Type of task return value. |
| 41 | * |
| 42 | * @return Thread future. |
| 43 | */ |
| 44 | @SuppressWarnings("unchecked") |
| 45 | public static <T> Future<T> run(Task<T> action) { |
| 46 | return (Future<T>) service.submit(action); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param updateInterval |
| 51 | * Time in milliseconds between each execution. |
| 52 | * @param action |
| 53 | * Runnable to start in new thread. |
| 54 | * |
| 55 | * @return Scheduled future. |
| 56 | */ |
| 57 | public static ScheduledFuture<?> runRepeated(long updateInterval, Runnable action) { |
| 58 | return scheduledService.scheduleAtFixedRate(action, 0, updateInterval, |
| 59 | TimeUnit.MILLISECONDS); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param time |
| 64 | * Delay to wait in milliseconds. |
| 65 | * @param action |
| 66 | * Runnable to start in new thread. |
| 67 | * |
| 68 | * @return Scheduled future. |
| 69 | */ |
| 70 | public static Future<?> runDelayed(long time, Runnable action) { |
| 71 | return scheduledService.schedule(action, time, TimeUnit.MILLISECONDS); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Run a given action with a timeout. |
nothing calls this directly
no test coverage detected