1.ExecutorManager
用于创建线程池,
- 有一个默认线程池
- allocateExecutor用于分配新线程池
- getExecutor获取已经分配的线程池,否则用默认的线程池
public interface ExecutorManager {
String DEFAULT_EXECUTOR_ID = "_default_executor";
ThreadPoolExecutor getDefaultExecutor();
ThreadPoolExecutor getExecutor(String var1);
void allocateExecutor(String var1, int var2, int var3);
List<String> getExecutorIDs();
}
示例代码:
public class ExecutorManagerTest {
public static void main(String[] args) {
ExecutorManager service = HSFServiceContainer.getInstance(ExecutorManager.class);
service.getDefaultExecutor().execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
service.allocateExecutor("test", 2, 2);
service.getExecutor("test").execute(new Runnable() {
public void run() {
System.out.println(Thread.currentThread().getName());
}
});
}
}
2.ThreadPoolService
可以理解为适用于不同场景的线程池集合
public interface ThreadPoolService {
List<String> ownThreadPoolServiceUniqueNames();
ThreadPoolInfo getBizThreadPoolInfo(String var1);
ThreadPoolInfo getDefaultBizThreadPoolInfo();
ExecutorManager getExecutorManager();
}
以上4 个方法是对ExecutorManager接口的扩展,可以获取线程池信息
public interface ThreadPoolService {
Executor callbackExecutor();
Executor internalCallbackExecutor();
ScheduledExecutorService nextScheduledExecutor();
ScheduledExecutorService nextExportReferExecutor();
Executor getHttpNotifierExecutor();
List<ScheduledExecutorService> listScheduledExecutors();
}
以上方法则根据场景不同,使用不同线程池
示例代码:
public class ThreadPoolServiceTest {
public static void main(String[] args) {
ThreadPoolService service = HSFServiceContainer.getInstance(ThreadPoolService.class);
System.out.println(service.getDefaultBizThreadPoolInfo());
service.getExecutorManager().allocateExecutor("test", 2, 2);
System.out.println(service.getBizThreadPoolInfo("test"));
service.getHttpNotifierExecutor().execute(new Runnable() {
public void run() {
System.out.println("test http");
}
});
}
}
网友评论