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

com.atomikos.thread.TaskManager Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * Copyright (C) 2000-2020 Atomikos 
 *
 * LICENSE CONDITIONS
 *
 * See http://www.atomikos.com/Main/WhichLicenseApplies for details.
 */

package com.atomikos.thread;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.atomikos.logging.Logger;
import com.atomikos.logging.LoggerFactory;

/**
 * Scheduling logic for tasks/threads.
 */

public enum TaskManager {
	SINGLETON;
	
	private static final Logger LOGGER = LoggerFactory.createLogger(TaskManager.class);
	
	private ThreadPoolExecutor executor;
	

	private void init() {
		SynchronousQueue synchronousQueue = new SynchronousQueue();
		executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, new Long(60L),
				TimeUnit.SECONDS, synchronousQueue, new AtomikosThreadFactory());

	}

	/**
	 * Notification of shutdown to close all pooled threads.
	 * 
	 */
	public synchronized void shutdown() {
		if (executor != null) {
			executor.shutdown();
			executor = null;
		}
	}

	/**
	 * Schedules a task for execution by a thread.
	 * 
	 * @param task
	 */
	public void executeTask(Runnable task) {
		if (executor == null) {
			// happens on restart of TS within same VM
			init();
		}
		executor.execute(task);
	}

	private static class AtomikosThreadFactory implements
			java.util.concurrent.ThreadFactory {

		private volatile AtomicInteger count = new AtomicInteger(0);
		private final ThreadGroup group;

		private AtomikosThreadFactory() {
			SecurityManager sm = System.getSecurityManager();
			group = (sm != null ? sm.getThreadGroup() : Thread.currentThread()
					.getThreadGroup());
		}

		@Override
		public Thread newThread(Runnable r) {
			String realName = "Atomikos:" + count.incrementAndGet();
			if (LOGGER.isTraceEnabled())
				LOGGER.logTrace("ThreadFactory: creating new thread: "+ realName);
			Thread thread = new Thread(group, r, realName);
			thread.setContextClassLoader(Thread.currentThread().getContextClassLoader());
			thread.setDaemon(true);
			return thread;
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy