package com.badlogic.gdx.utils.async;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
 * Allows asnynchronous execution of {@link AsyncTask} instances on a separate thread.
 * Needs to be disposed via a call to {@link #dispose()} when no longer used, in which
 * case the executor waits for running tasks to finish. Scheduled but not yet
 * running tasks will not be executed. 
 * @author badlogic
 *
 */
public class AsyncExecutor implements Disposable {
	private final ExecutorService executor;
	
	/**
	 * Creates a new AsynchExecutor that allows maxConcurrent
	 * {@link Runnable} instances to run in parallel.
	 * @param maxConcurrent
	 */
	public AsyncExecutor(int maxConcurrent) {
		executor = Executors.newFixedThreadPool(maxConcurrent, new ThreadFactory() {
			@Override
			public Thread newThread (Runnable r) {
				Thread thread = new Thread(r, "AsynchExecutor-Thread");
				thread.setDaemon(true);
				return thread;
			}
		});
	}
	
	/**
	 * Submits a {@link Runnable} to be executed asynchronously. If
	 * maxConcurrent runnables are already running, the runnable 
	 * will be queued.
	 * @param task the task to execute asynchronously
	 */
	public <T> AsyncResult<T> submit(final AsyncTask<T> task) {
		return new AsyncResult(executor.submit(new Callable<T>() {
			@Override
			public T call () throws Exception {
				return task.call();
			}
		}));
	}
	
	/**
	 * Waits for running {@link AsyncTask} instances to finish,
	 * then destroys any resources like threads. Can not be used
	 * after this method is called.
	 */
	@Override
	public void dispose () {
		executor.shutdown();
		try {
			executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			new GdxRuntimeException("Couldn't shutdown loading thread");
		}
	}
}

@@ -1954,16 +1954,22 @@
 e) %7B%0A%09%09%09
+throw 
 new GdxR
@@ -2014,24 +2014,27 @@
 ding thread%22
+, e
 );%0A%09%09%7D%0A%09%7D%0A%7D%0A

fix not thrown exception, pass inner exception