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

de.thksystems.util.concurrent.NamedRunnable Maven / Gradle / Ivy

Go to download

Commons for lang, crypto, xml, dom, text, csv, reflection, annotations, parsing, ...

There is a newer version: 4.4.0
Show newest version
package de.thksystems.util.concurrent;

/**
 * A {@link Runnable} with the ability to set the thread-name that is used while running.
 */
public class NamedRunnable implements Runnable {

	private final Runnable runnable;
	private String threadName;

	private NamedRunnable(Runnable runnable) {
		this.runnable = runnable;
	}

	@Override
	public void run() {
		String oldThreadName = Thread.currentThread().getName();
		try {
			Thread.currentThread().setName(threadName);
			runnable.run();
		} finally {
			Thread.currentThread().setName(oldThreadName);
		}
	}

	public static NamedRunnable of(Runnable runnable) {
		return new NamedRunnable(runnable);
	}

	public NamedRunnable withThreadName(String threadName) {
		this.threadName = threadName;
		return this;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy