All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java.util.concurrent.ThreadPoolExecutor Maven / Gradle / Ivy

Go to download

JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.

There is a newer version: 0.6.8
Show newest version
package java.util.concurrent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ThreadPoolExecutor implements ExecutorService {

	private static final int TASK_WAITING    = 1;
	private static final int TASK_RUNNING    = 2;
	private static final int TASK_STOPPING    = 3;

	private static final int EX_WORKING       = 1;
	private static final int EX_STOP       = 2;
	private static final int EX_SHUTDOWN   = 3;

	private static final int MAX_POOL_SIZE = Integer.MAX_VALUE / 2;

	private int state = EX_WORKING;
	private int poolSize;
	private int maxPoolSize;
	final private BlockingQueue workQueue;
	private ThreadFactory factory;
	private RejectedExecutionHandler rejectHandler;

	private ArrayList taskPool;

	public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, null, null);
	}

	public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, null);
	}

	public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) {
		this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, null, handler);
	}

	public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
		if (corePoolSize < 0 ||
			maximumPoolSize <= 0 ||
			maximumPoolSize < corePoolSize)
				throw new IllegalArgumentException();
		poolSize = corePoolSize;
		maxPoolSize = Math.min(maximumPoolSize, MAX_POOL_SIZE);
		this.workQueue = workQueue == null ? new LinkedBlockingDeque<>() : workQueue;
		factory = threadFactory == null ? new Executors.DefaultThreadFactory() : threadFactory;
		rejectHandler = handler;

		taskPool = new ArrayList<>();
		for (int i = 0; i < poolSize; i++){
			taskPool.add(new Task());
		}
	}

	@Override
	public void shutdown() {
		state = EX_SHUTDOWN;
	}

	@Override
	public List shutdownNow() {
		state = EX_SHUTDOWN;
		for (Task t : taskPool) {
			if (t.state == TASK_RUNNING)
				t.state = TASK_STOPPING;
		}
		List l;
		synchronized (workQueue) {
			l = new ArrayList<>(workQueue);
			workQueue.clear();
		}
		return l;
	}

	@Override
	public boolean isShutdown() {
		return state == EX_SHUTDOWN;
	}

	@Override
	public boolean isTerminated() {
		return state == EX_SHUTDOWN;
	}

	@Override
	public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
		return true;
	}

	@Override
	public  Future submit(Callable task) {
		try {
			return new ImmediateFuture(task.call());
		} catch (Exception e) {
			return new ImmediateFuture(null);
		}
	}

	@Override
	public  Future submit(Runnable task, T result) {
		task.run();
		return new ImmediateFuture(result);
	}

	@Override
	public Future submit(Runnable task) {
		return submit(task, null);
	}

	@Override
	public  List> invokeAll(Collection> tasks) throws InterruptedException {
		ArrayList> out = new ArrayList>();
		for (Callable task : tasks) {
			try {
				out.add(new ImmediateFuture(task.call()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return out;
	}

	@Override
	public  List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException {
		return invokeAll(tasks);
	}

	@Override
	public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException {
		for (Callable task : tasks) {
			try {
				return task.call();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
		return invokeAny(tasks);
	}

	@Override
	public void execute(Runnable command) {
		if (state != EX_WORKING) {
			if (rejectHandler != null)
				rejectHandler.rejectedExecution(command, this);
			return;
		}

		synchronized (workQueue) {
			try {
				workQueue.put(command);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		execute();
	}

	private void execute(){
		for (Task t : taskPool){
			if (t.state == TASK_WAITING){
				factory.newThread(t).start();
				return;
			}
		}
		if (poolSize < maxPoolSize) {
			poolSize++;
			taskPool.add(new Task());
			execute();
		}
	}

	private class Task implements Runnable {
		public int state = TASK_WAITING;
		@Override
		public void run() {
			state = TASK_RUNNING;
			while (state < TASK_STOPPING) {
				Runnable command;
				synchronized (workQueue) {
					command = workQueue.poll();
				}
				if (command != null) {
					command.run();
				} else {
					state = TASK_STOPPING;
				}
			}
			state = TASK_WAITING;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy