-
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.
Merge pull request #25 from lunasaw/2.5.7
2.5.7 线程池优化
- Loading branch information
Showing
3 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/luna/common/thread/NamedThreadFactory.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,38 @@ | ||
package com.luna.common.thread; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* thread 命名 | ||
* | ||
* @author luna | ||
**/ | ||
public class NamedThreadFactory implements ThreadFactory { | ||
private static final AtomicInteger poolNumber = new AtomicInteger(1); | ||
|
||
private final AtomicInteger threadNumber = new AtomicInteger(1); | ||
private final ThreadGroup group; | ||
private final String namePrefix; | ||
private final boolean isDaemon; | ||
|
||
public NamedThreadFactory(String name) { | ||
this(name, false); | ||
} | ||
|
||
public NamedThreadFactory(String prefix, boolean daemon) { | ||
SecurityManager s = System.getSecurityManager(); | ||
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); | ||
namePrefix = prefix + "-" + poolNumber.getAndIncrement() + "-thread-"; | ||
isDaemon = daemon; | ||
} | ||
|
||
public Thread newThread(Runnable runnable) { | ||
Thread t = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0); | ||
// Default value is parent thread's | ||
t.setContextClassLoader(NamedThreadFactory.class.getClassLoader()); | ||
t.setPriority(Thread.MAX_PRIORITY); | ||
t.setDaemon(isDaemon); | ||
return t; | ||
} | ||
} |