
org.ow2.petals.microkernel.util.PetalsAbstractThreadFactory Maven / Gradle / Ivy
/**
* Copyright (c) 2009-2012 Capgemini, 2009-2012 EBM WebSourcing, 2012-2016 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.microkernel.util;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import com.ebmwebsourcing.easycommons.log.LoggingUtil;
/**
* @author Christophe DENEUX - Capgemini Sud/Linagora
*/
public abstract class PetalsAbstractThreadFactory implements ThreadFactory {
protected final LoggingUtil log;
protected final ThreadGroup group;
protected final AtomicInteger threadId = new AtomicInteger(0);
/**
* Thread base name to which a counter value will be added: baseNameXX
*/
protected final String baseName;
/**
* @param baseName
* Thread base name.
*/
public PetalsAbstractThreadFactory(final String baseName, final LoggingUtil log) {
this.group = Thread.currentThread().getThreadGroup();
this.baseName = baseName;
this.log = log;
}
public Thread newThread(final Runnable runnable) {
final Thread thread = new Thread(this.group, runnable, this.baseName + this.threadId.getAndIncrement());
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread t, final Throwable e) {
PetalsAbstractThreadFactory.this.log.error("Thread '" + t.getName() + "' threw an uncaught exception: "
+ e.getMessage(), e);
}
});
return thread;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy