de.thksystems.util.concurrent.NamedSupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cumin Show documentation
Show all versions of cumin Show documentation
Commons for lang, crypto, xml, dom, text, csv, reflection, annotations, parsing, ...
/*
* tksCommons
*
* Author : Thomas Kuhlmann (ThK-Systems, http://www.thk-systems.de)
* License : LGPL (https://www.gnu.org/licenses/lgpl.html)
*/
package de.thksystems.util.concurrent;
import java.util.function.Supplier;
/**
* A {@link Supplier} with the ability to set the thread name that is used while calling.
*/
public class NamedSupplier implements Supplier {
private final Supplier supplier;
private String threadName;
protected NamedSupplier(Supplier supplier) {
this.supplier = supplier;
}
public static NamedSupplier of(Supplier supplier) {
return new NamedSupplier<>(supplier);
}
@Override
public V get() {
String oldThreadName = Thread.currentThread().getName();
try {
Thread.currentThread().setName(threadName.replace("{thread-id}", String.valueOf(Thread.currentThread().getId())));
return supplier.get();
} finally {
Thread.currentThread().setName(oldThreadName);
}
}
}