Skip to content

Commit

Permalink
⚡ add PanGuThreadFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
yuebaix committed Sep 24, 2021
1 parent 4daff0e commit cb15ada
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yuebaix.pangu.common.util;

public class StringUtil {
/**
* <pre>
* StringUtil.isEmpty(null) = true
* StringUtil.isEmpty("") = true
* StringUtil.isEmpty(" ") = false
* StringUtil.isEmpty("bob") = false
* StringUtil.isEmpty(" bob ") = false
* </pre>
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}

public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yuebaix.pangu.core.concurrent;

public class PanGuConcurrentKit {
private PanGuConcurrentKit() {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
package com.yuebaix.pangu.core.concurrent;

public class PanGuThreadFactory {
import com.yuebaix.pangu.common.util.StringUtil;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class PanGuThreadFactory implements ThreadFactory {
private static final String EMPTY_STR = "";
private static final String THREAD_NAME_SP = "-";
private static final String DEFAULT_THREAD_GROUP_NAME = "PanGu";

private final ThreadGroup threadGroup;
private final String threadNamePrefix;
private final boolean daemon;
private final int priority;

private static final AtomicInteger threadPoolNumber = new AtomicInteger(1);
private final AtomicInteger threadNumber = new AtomicInteger(1);

public PanGuThreadFactory(String threadGroupName) {
this(threadGroupName, null, false, -1);
}

public PanGuThreadFactory(String threadGroupName, boolean daemon, int priority) {
this(threadGroupName, null, daemon, priority);
}

public PanGuThreadFactory(String threadGroupName, String threadNamePrefix) {
this(threadGroupName, threadNamePrefix, false, -1);
}

public PanGuThreadFactory(String threadGroupName, String threadNamePrefix, boolean daemon, int priority) {
if (StringUtil.isEmpty(threadGroupName)) {
threadGroupName = DEFAULT_THREAD_GROUP_NAME;
}
if (StringUtil.isEmpty(threadNamePrefix)) {
threadNamePrefix = String.join(THREAD_NAME_SP,
threadGroupName,
EMPTY_STR + threadPoolNumber.getAndIncrement(),
this.getClass().getSimpleName(),
EMPTY_STR);
}
this.threadGroup = new ThreadGroup(threadGroupName);
this.threadNamePrefix = threadNamePrefix;
this.daemon = daemon;
this.priority = priority;
}

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(threadGroup, r, threadNamePrefix + threadNumber.getAndIncrement(), 0);
if (daemon) {
if (!t.isDaemon()) {
t.setDaemon(true);
}
} else {
if (t.isDaemon()) {
t.setDaemon(false);
}
}
if (priority > 0 && t.getPriority() != priority) {
t.setPriority(priority);
}
return t;
}
}

0 comments on commit cb15ada

Please sign in to comment.