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

com.github.thorbenkuck.keller.mvp.ThreadPoolCache Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.keller.mvp;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

class ThreadPoolCache {

	private static ExecutorService executorService;
	private static final Semaphore semaphore = new Semaphore(1);

	static ExecutorService getExecutorService() {
		return lazyGet();
	}

	private static ExecutorService lazyGet() {
		ExecutorService returnValue;
		try {
			semaphore.acquire();

			if(executorService == null || executorService.isShutdown()) {
				executorService = Executors.newCachedThreadPool();
			}
			returnValue = executorService;
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new IllegalStateException("Creation of ExecutorService failed!");
		} finally {
			semaphore.release();
		}

		return returnValue;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy