|
| 1 | +package com.jme3.terrain.executor; |
| 2 | + |
| 3 | +import java.util.concurrent.*; |
| 4 | +import java.util.concurrent.atomic.AtomicInteger; |
| 5 | + |
| 6 | +/** |
| 7 | + * The class to provide single executor service to run background tasks of terrain staff. |
| 8 | + * |
| 9 | + * @author JavaSaBr |
| 10 | + */ |
| 11 | +public class TerrainExecutorService { |
| 12 | + |
| 13 | + private static final Runtime RUNTIME = Runtime.getRuntime(); |
| 14 | + |
| 15 | + /** |
| 16 | + * The constructor of the terrain executor service. |
| 17 | + */ |
| 18 | + private static volatile Callable<ExecutorService> constructor = new Callable<ExecutorService>() { |
| 19 | + |
| 20 | + @Override |
| 21 | + public ExecutorService call() throws Exception { |
| 22 | + return Executors.newFixedThreadPool(RUNTIME.availableProcessors(), new ThreadFactory() { |
| 23 | + |
| 24 | + private final AtomicInteger counter = new AtomicInteger(-1); |
| 25 | + |
| 26 | + @Override |
| 27 | + public Thread newThread(final Runnable task) { |
| 28 | + final Thread thread = new Thread(task); |
| 29 | + thread.setName("jME3 Terrain Thread [" + counter.incrementAndGet() + "]"); |
| 30 | + thread.setDaemon(true); |
| 31 | + return thread; |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * Set a new constructor of executor service to provide other implementation. |
| 39 | + * |
| 40 | + * @param constructor the constructor. |
| 41 | + */ |
| 42 | + private static void setConstructor(final Callable<ExecutorService> constructor) { |
| 43 | + TerrainExecutorService.constructor = constructor; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * https://stackoverflow.com/questions/29883403/double-checked-locking-without-volatile |
| 48 | + * <p> |
| 49 | + * This suggestion is of Aleksey Shipilev |
| 50 | + */ |
| 51 | + private static class LazyInitializer { |
| 52 | + public final TerrainExecutorService instance; |
| 53 | + public LazyInitializer(final TerrainExecutorService instance) { |
| 54 | + this.instance = instance; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * The lazy singleton. |
| 60 | + */ |
| 61 | + private static LazyInitializer initializer; |
| 62 | + |
| 63 | + public static TerrainExecutorService getInstance() { |
| 64 | + |
| 65 | + LazyInitializer lazy = initializer; |
| 66 | + |
| 67 | + if (lazy == null) { // check 1 |
| 68 | + synchronized (TerrainExecutorService.class) { |
| 69 | + lazy = initializer; |
| 70 | + if (lazy == null) { // check2 |
| 71 | + lazy = new LazyInitializer(new TerrainExecutorService()); |
| 72 | + initializer = lazy; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return lazy.instance; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The implementation of executor service. |
| 82 | + */ |
| 83 | + private final ExecutorService executorService; |
| 84 | + |
| 85 | + private TerrainExecutorService() { |
| 86 | + try { |
| 87 | + this.executorService = constructor.call(); |
| 88 | + } catch (final Exception e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public <T> Future<T> submit(final Callable<T> task) { |
| 94 | + return executorService.submit(task); |
| 95 | + } |
| 96 | + |
| 97 | + public <T> Future<T> submit(final Runnable task, final T result) { |
| 98 | + return executorService.submit(task, result); |
| 99 | + } |
| 100 | + |
| 101 | + public Future<?> submit(final Runnable task) { |
| 102 | + return executorService.submit(task); |
| 103 | + } |
| 104 | + |
| 105 | + public void execute(final Runnable command) { |
| 106 | + executorService.execute(command); |
| 107 | + } |
| 108 | +} |
0 commit comments