cn.hippo4j.common.design.builder.ThreadFactoryBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hippo4j-common Show documentation
Show all versions of hippo4j-common Show documentation
HIPPO4J、HIPPO4J-CORE 公共代码库.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.common.design.builder;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;
/**
* Thread-factory builder.
*/
public class ThreadFactoryBuilder implements Builder {
private static final long serialVersionUID = 1L;
private ThreadFactory backingThreadFactory;
private String namePrefix;
private Boolean daemon;
private Integer priority;
private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
public ThreadFactoryBuilder threadFactory(ThreadFactory backingThreadFactory) {
this.backingThreadFactory = backingThreadFactory;
return this;
}
public ThreadFactoryBuilder prefix(String namePrefix) {
this.namePrefix = namePrefix;
return this;
}
public ThreadFactoryBuilder daemon(boolean daemon) {
this.daemon = daemon;
return this;
}
public ThreadFactoryBuilder priority(int priority) {
if (priority < Thread.MIN_PRIORITY) {
throw new IllegalArgumentException(String.format("Thread priority ({}) must be >= {}", priority, Thread.MIN_PRIORITY));
}
if (priority > Thread.MAX_PRIORITY) {
throw new IllegalArgumentException(String.format("Thread priority ({}) must be <= {}", priority, Thread.MAX_PRIORITY));
}
this.priority = priority;
return this;
}
public void uncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
this.uncaughtExceptionHandler = uncaughtExceptionHandler;
}
public static ThreadFactoryBuilder builder() {
return new ThreadFactoryBuilder();
}
@Override
public ThreadFactory build() {
return build(this);
}
private static ThreadFactory build(ThreadFactoryBuilder builder) {
final ThreadFactory backingThreadFactory = (null != builder.backingThreadFactory)
? builder.backingThreadFactory
: Executors.defaultThreadFactory();
final String namePrefix = builder.namePrefix;
final Boolean daemon = builder.daemon;
final Integer priority = builder.priority;
final Thread.UncaughtExceptionHandler handler = builder.uncaughtExceptionHandler;
final AtomicLong count = (null == namePrefix) ? null : new AtomicLong();
return r -> {
final Thread thread = backingThreadFactory.newThread(r);
if (null != namePrefix) {
thread.setName(namePrefix + "_" + count.getAndIncrement());
}
if (null != daemon) {
thread.setDaemon(daemon);
}
if (null != priority) {
thread.setPriority(priority);
}
if (null != handler) {
thread.setUncaughtExceptionHandler(handler);
}
return thread;
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy