001/* 002 * Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining 005 * a copy of this software and associated documentation files (the 006 * "Software"), to deal in the Software without restriction, including 007 * without limitation the rights to use, copy, modify, merge, publish, 008 * distribute, sublicense, and/or sell copies of the Software, and to 009 * permit persons to whom the Software is furnished to do so, subject to 010 * the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be 013 * included in all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 016 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 018 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 019 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 020 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 021 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 022 */ 023 024package co.aikar.taskchain; 025 026import java.util.concurrent.Executors; 027import java.util.concurrent.ThreadPoolExecutor; 028import java.util.concurrent.TimeUnit; 029import java.util.concurrent.atomic.AtomicInteger; 030 031@SuppressWarnings("WeakerAccess") 032public class TaskChainAsyncQueue implements AsyncQueue { 033 private static final AtomicInteger threadId = new AtomicInteger(); 034 private final ThreadPoolExecutor executor; 035 036 public TaskChainAsyncQueue() { 037 this.executor = createCachedThreadPool(); 038 } 039 040 public TaskChainAsyncQueue(ThreadPoolExecutor executor) { 041 this.executor = executor; 042 } 043 044 public static ThreadPoolExecutor createCachedThreadPool() { 045 return (ThreadPoolExecutor) Executors.newCachedThreadPool(r -> { 046 final Thread thread = new Thread(r); 047 thread.setName("TaskChainAsyncQueue Thread " + threadId.getAndIncrement()); 048 return thread; 049 }); 050 } 051 052 public void postAsync(Runnable runnable) { 053 executor.submit(runnable); 054 } 055 056 /** 057 * Call during game shutdown state 058 * @param timeout 059 * @param unit 060 */ 061 public void shutdown(int timeout, TimeUnit unit) { 062 try { 063 executor.setRejectedExecutionHandler((r, executor1) -> r.run()); 064 executor.shutdown(); 065 executor.awaitTermination(timeout, unit); 066 } catch (InterruptedException e) { 067 e.printStackTrace(); 068 } 069 } 070}