thread-factory

A set of utilities for creating custom ThreadFactory objects

License

License

GroupId

GroupId

com.pivovarit
ArtifactId

ArtifactId

thread-factory
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

thread-factory
A set of utilities for creating custom ThreadFactory objects
Source Code Management

Source Code Management

https://github.com/pivovarit/thread-factory

Download thread-factory

How to add to project

<!-- https://jarcasting.com/artifacts/com.pivovarit/thread-factory/ -->
<dependency>
    <groupId>com.pivovarit</groupId>
    <artifactId>thread-factory</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.pivovarit/thread-factory/
implementation 'com.pivovarit:thread-factory:0.0.1'
// https://jarcasting.com/artifacts/com.pivovarit/thread-factory/
implementation ("com.pivovarit:thread-factory:0.0.1")
'com.pivovarit:thread-factory:jar:0.0.1'
<dependency org="com.pivovarit" name="thread-factory" rev="0.0.1">
  <artifact name="thread-factory" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.pivovarit', module='thread-factory', version='0.0.1')
)
libraryDependencies += "com.pivovarit" % "thread-factory" % "0.0.1"
[com.pivovarit/thread-factory "0.0.1"]

Dependencies

test (3)

Group / Artifact Type Version
org.slf4j : slf4j-simple jar 1.7.5
org.junit.jupiter : junit-jupiter-engine jar 5.3.2
org.assertj : assertj-core jar 3.8.0

Project Modules

There are no modules declared in this project.

thread-factory

Build Status License Maven Central

Rationale

Custom thread pool naming is critical for debuggability.

Unfortunately, relying on default naming schemes often leads to situations where most of our threads are named in a cryptic manner:

Alt text

Using core Java, there's no easy way to create custom ThreadFactory instances.

Basic API

  • ThreadFactories.prefixed(String prefix)

  • ThreadFactories.prefixed(String prefix, ThreadFactory base)

  • ThreadFactories.suffixed(String suffix)

  • ThreadFactories.suffixed(String suffix, ThreadFactory base)

  • ThreadFactories.builder(String nameFormat)

    • withDaemonThreads(boolean daemon)
    • withUncaughtExceptionHandler(UncaughtExceptionHandler handler)
    • fromThreadFactory(ThreadFactory backingThreadFactory)
    • build()

Maven Dependencies

<dependency>
    <groupId>com.pivovarit</groupId>
    <artifactId>thread-factory</artifactId>
    <version>0.0.1</version>
</dependency>
Gradle
compile 'com.pivovarit:thread-factory:0.0.1'

Examples

Tips

Dependencies

None - the library is implemented using core Java libraries.

Version history

0.0.1 (23-01-2019)

  • Initial project version

What if I don't want to add a new library just for the sake of prefixing threads?

That's reasonable. Just copy over the following code and use at will:

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author Grzegorz Piwowarek
 */
public class CustomThreadFactory implements ThreadFactory {

    private String prefix;
    private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

    CustomThreadFactory(String prefix) {
        this.prefix = prefix;
    }

    @Override
    public Thread newThread(Runnable task) {
        Thread thread = defaultThreadFactory.newThread(task);
        thread.setName(prefix + "-" + thread.getName());
        return thread;
    }
}

Versions

Version
0.0.1