-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
pangu-common/src/main/java/com/yuebaix/pangu/common/util/StringUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
pangu-core/src/main/java/com/yuebaix/pangu/core/concurrent/PanGuConcurrentKit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
65 changes: 64 additions & 1 deletion
65
pangu-core/src/main/java/com/yuebaix/pangu/core/concurrent/PanGuThreadFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |