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

de.thksystems.util.concurrent.NamedFunction 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
/*
 * 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.Function;

/**
 * A {@link Function} with the ability to set the thread name that is used while calling.
 */
public class NamedFunction implements Function {

    private final Function function;
    private String threadName;

    protected NamedFunction(Function function) {
        this.function = function;
    }

    public static  NamedFunction of(Function function) {
        return new NamedFunction<>(function);
    }

    @Override
    public R apply(V t) {
        String oldThreadName = Thread.currentThread().getName();
        try {
            Thread.currentThread().setName(threadName.replace("{thread-id}", String.valueOf(Thread.currentThread().getId())));
            return function.apply(t);
        } finally {
            Thread.currentThread().setName(oldThreadName);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy