



##  简介

&ensp;&ensp;&ensp;&ensp;统一线程池管理提供线程使用生命周期管理，在用户使用完线程返回线程池时，统一会线程变量清理和资源回收等工作，在苍穹中使用新线程或线程池，需要使用线程池管理提供。  

##   特性

- 为什么要使用平台封装后的线程池
  - 统一管理和监控，比如监控总共创建了多少个线程，当前活动线程数
  - 避免线程变量干扰，使用完线程返回线程池时，统一会线程变量清理和资源回收等工作
  - 可传递requestcontext上下文到线程池线程中
  - 相对jdk线程池做了更多优化，当线程池任务满时，不丢任务，更符合我们erp业务场景
  - 做了限制，数据访问必须在被管理线程中运行，即线程池创建线程。


##  快速上手
&ensp;&ensp;&ensp;&ensp;通过ThreadPools 创建线程池

### 执行一次

 ```java
ThreadPools.executeOnce(String name, Runnable runnable);
ThreadPools.executeOnceIncludeRequestContext(String name, Runnable runnable);
ThreadPools.executeOnceIncludeRequestContext(String name, Runnable runnable, RequestContext rc)
 ```

### 创建线程池

- 不能创建相同名词的线程池

- 获取到的线程池需要全局性，如静态变量

  ```java
  private static ThreadPool pool = ThreadPools.newCachedThreadPool(“Cacal",3,30);
  ```

- 有两类接口：new\*\*\*ThreadPool， new\*\*\*ExecutorService。new\*\*\*ThreadPool返回ThreadPool接口，可以携带RequestContext到线程池中的线程变量中。 new\*\*\*ExecutorService返回jdk的ExecutorService接口。

####      固定线程池

固定线程池用于线程个数确定的场景。

  ```java
ThreadPools.newExecutorService(String poolName, int nThreads);
ThreadPools.newFixedThreadPool(String poolName, int nThreads);
  ```

#### Cache线程池

cache线程池创建建议明确指定 最小线程数和最大线程数，最小线程数需要大于0，最大线程不能超过10000.

```java
ThreadPools.newCachedThreadPool(String poolName);//
ThreadPools.newCachedThreadPool(String poolName, int coreThread, int maxThread);  
ThreadPools.newCachedExecutorService(String poolName);
ThreadPools.newCachedExecutorService(String poolName, int coreThread, int maxThread);
```

### ThreadPool API

  ```java
  public interface ThreadPool {
  	/**
  	 * Executes the given command at some time in the future. The command may
  	 * execute in a new thread, in a pooled thread, or in the calling thread, at
  	 * the discretion of the {@code Executor} implementation.
  	 *
  	 * @param command
  	 *            the runnable task
  	 * @throws RejectedExecutionException
  	 *             if this task cannot be accepted for execution
  	 * @throws NullPointerException
  	 *             if command is null
  	 */
  	void execute(Runnable command);
  
  	/**
  	 * Executes the given command at some time in the future with current
  	 * thread's RequestContext
  	 * 
  	 * @param command
  	 * @param includeRequestContext
  	 */
  	void executeIncludeRequestContext(Runnable command);
  
  	void executeIncludeRequestContext(Runnable command, RequestContext rc);
  
  	/**
  	 * Submits a value-returning task for execution and returns a Future
  	 * representing the pending results of the task. The Future's {@code get}
  	 * method will return the task's result upon successful completion.
  	 *
  	 * <p>
  	 * If you would like to immediately block waiting for a task, you can use
  	 * constructions of the form {@code result = exec.submit(aCallable).get();}
  	 *
  	 * @param task
  	 *            the task to submit
  	 * @param <T>
  	 *            the type of the task's result
  	 * @return a Future representing pending completion of the task
  	 * @throws RejectedExecutionException
  	 *             if the task cannot be scheduled for execution
  	 * @throws NullPointerException
  	 *             if the task is null
  	 */
  	<T> Future<T> submit(Callable<T> task);
  
  	public void close();
  }
  ```

  

###  


