forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE alibaba#2856][module:core、consistency]Adjust the use of thread…
… pools Change-Id: I793770ca0f287157dc88e3ecf1147ea308e4a547
- Loading branch information
1 parent
7c4cca4
commit 315cf81
Showing
7 changed files
with
155 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,70 +27,169 @@ | |
/** | ||
* Unified thread pool creation factory, and actively create thread | ||
* pool resources by ThreadPoolManager for unified life cycle management | ||
* {@link ExecutorFactory.Managed} | ||
* | ||
* Unified thread pool creation factory without life cycle management | ||
* {@link ExecutorFactory} | ||
* | ||
* @author <a href="mailto:[email protected]">liaochuntao</a> | ||
*/ | ||
@SuppressWarnings("PMD.ThreadPoolCreationRule") | ||
public final class ExecutorFactory { | ||
|
||
private static final ThreadPoolManager THREAD_POOL_MANAGER = ThreadPoolManager.getInstance(); | ||
public static ExecutorService newSingleExecutorService() { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
return executorService; | ||
} | ||
|
||
public static ExecutorService newSingleExecutorService(final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory); | ||
return executorService; | ||
} | ||
|
||
public static final String DEFAULT_NAMESPACE = "nacos"; | ||
public static ExecutorService newFixExecutorService(final int nThreads) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads); | ||
return executorService; | ||
} | ||
|
||
public static ExecutorService newFixExecutorService(final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads, threadFactory); | ||
return executorService; | ||
} | ||
|
||
public static ScheduledExecutorService newSingleScheduledExecutorService(final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, threadFactory); | ||
return executorService; | ||
} | ||
|
||
public static ScheduledExecutorService newScheduledExecutorService( final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(nThreads, threadFactory); | ||
return executorService; | ||
} | ||
|
||
public static ThreadPoolExecutor newCustomerThreadExecutor( | ||
final int coreThreads, | ||
final int maxThreads, | ||
final long keepAliveTimeMs, | ||
final ThreadFactory threadFactory) { | ||
ThreadPoolExecutor executor = new ThreadPoolExecutor(coreThreads, maxThreads, | ||
keepAliveTimeMs, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue<Runnable>(), | ||
threadFactory); | ||
return executor; | ||
} | ||
//TODO remove Deprecated function after replace all module | ||
@Deprecated | ||
public static ExecutorService newSingleExecutorService(final String group) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ExecutorService newSingleExecutorService(final String group, | ||
final ThreadFactory threadFactory) { | ||
final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ExecutorService newFixExecutorService(final String group, | ||
final int nThreads) { | ||
final int nThreads) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ExecutorService newFixExecutorService(final String group, | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ScheduledExecutorService newSingleScheduledExecutorService(final String group, | ||
final ThreadFactory threadFactory) { | ||
final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ScheduledExecutorService newScheduledExecutorService(final String group, | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(nThreads, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
@Deprecated | ||
public static ThreadPoolExecutor newCustomerThreadExecutor(final String group, | ||
final int coreThreads, | ||
final int maxThreads, | ||
final long keepAliveTimeMs, | ||
final ThreadFactory threadFactory) { | ||
ThreadPoolExecutor executor = new ThreadPoolExecutor(coreThreads, maxThreads, | ||
keepAliveTimeMs, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue<Runnable>(), | ||
threadFactory); | ||
return executor; | ||
} | ||
|
||
public final static class Managed{ | ||
public static final String DEFAULT_NAMESPACE = "nacos"; | ||
|
||
private static final ThreadPoolManager THREAD_POOL_MANAGER = ThreadPoolManager.getInstance(); | ||
|
||
public static ExecutorService newSingleExecutorService(final String group) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ExecutorService newSingleExecutorService(final String group, | ||
final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(1, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ExecutorService newFixExecutorService(final String group, | ||
final int nThreads) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ExecutorService newFixExecutorService(final String group, | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ScheduledExecutorService newSingleScheduledExecutorService(final String group, | ||
final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ScheduledExecutorService newScheduledExecutorService(final String group, | ||
final int nThreads, | ||
final ThreadFactory threadFactory) { | ||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(nThreads, threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService); | ||
return executorService; | ||
} | ||
|
||
public static ThreadPoolExecutor newCustomerThreadExecutor(final String group, | ||
final int coreThreads, | ||
final int maxThreads, | ||
final long keepAliveTimeMs, | ||
final ThreadFactory threadFactory) { | ||
ThreadPoolExecutor executor = new ThreadPoolExecutor(coreThreads, maxThreads, | ||
ThreadPoolExecutor executor = new ThreadPoolExecutor(coreThreads, maxThreads, | ||
keepAliveTimeMs, TimeUnit.MILLISECONDS, | ||
new LinkedBlockingQueue<Runnable>(), | ||
threadFactory); | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executor); | ||
return executor; | ||
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executor); | ||
return executor; | ||
} | ||
} | ||
|
||
} |
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
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 |
---|---|---|
|
@@ -18,18 +18,19 @@ | |
|
||
import com.alibaba.nacos.common.executor.ExecutorFactory; | ||
|
||
import com.alibaba.nacos.core.utils.ClassUtils; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">liaochuntao</a> | ||
*/ | ||
public final class ProtocolExecutor { | ||
|
||
private static final ExecutorService CP_MEMBER_CHANGE_EXECUTOR = ExecutorFactory.newSingleExecutorService( | ||
ProtocolManager.class.getName()); | ||
private static final ExecutorService CP_MEMBER_CHANGE_EXECUTOR = ExecutorFactory.Managed.newSingleExecutorService( | ||
ClassUtils.getCanonicalName(ProtocolManager.class)); | ||
|
||
private static final ExecutorService AP_MEMBER_CHANGE_EXECUTOR = ExecutorFactory.newSingleExecutorService( | ||
ProtocolManager.class.getName()); | ||
private static final ExecutorService AP_MEMBER_CHANGE_EXECUTOR = ExecutorFactory.Managed.newSingleExecutorService( | ||
ClassUtils.getCanonicalName(ProtocolManager.class)); | ||
|
||
public static void cpMemberChange(Runnable runnable) { | ||
CP_MEMBER_CHANGE_EXECUTOR.execute(runnable); | ||
|
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
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