定义两个类:
* 线程池配置类
* @author Administrator
*
*/
public class ThreadPoolConfig {
//池中所保存的线程数,包括空闲线程。
private int corePoolSize;
//池中允许的最大线程数。
private int maximumPoolSize;
//当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
private long keepAliveTime;
//时间单位名。
private String timeUnitName;
//任务队列的长度
private int queueSize;
//执行前用于保持任务的队列。此队列仅由保持 execute 方法提交的 Runnable 任务。
private BlockingQueue<Runnable> workQueue;
//用于设置创建线程的工厂
ThreadFactory threadFactory;
//由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序。
private RejectedExecutionHandler rejectedExecutionHandler;
public ThreadFactory getThreadFactory() {
if (threadFactory == null) {
synchronized (this) {
if (threadFactory == null) {
threadFactory = new ThreadFactory() {
public Thread newThread(Runnable r) {
return new Thread(r);
};
};
}
}
}
return threadFactory;
}
public void setThreadFactory(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
}
public int getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
}
public int getMaximumPoolSize() {
return maximumPoolSize;
}
public void setMaximumPoolSize(int maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
public long getKeepAliveTime() {
return keepAliveTime;
}
public void setKeepAliveTime(long keepAliveTime) {
this.keepAliveTime = keepAliveTime;
}
public TimeUnit getUnit() {
for (TimeUnit unit : TimeUnit.values()){
if (unit.name().equalsIgnoreCase(timeUnitName))
return unit;
}
return null;
}
public BlockingQueue<Runnable> getWorkQueue() {
return workQueue;
}
public void setWorkQueue(BlockingQueue<Runnable> workQueue) {
this.workQueue = workQueue;
}
public RejectedExecutionHandler getRejectedExecutionHandler() {
return rejectedExecutionHandler;
}
public void setRejectedExecutionHandler(
RejectedExecutionHandler rejectedExecutionHandler) {
this.rejectedExecutionHandler = rejectedExecutionHandler;
}
public int getQueueSize() {
return queueSize;
}
public void setQueueSize(int queueSize) {
this.queueSize = queueSize;
}
public String getTimeUnitName() {
return timeUnitName;
}
public void setTimeUnitName(String timeUnitName) {
this.timeUnitName = timeUnitName;
}
}
/**
* 线程池工厂类,getObject() 方法用来创建线程池对象
* @author Administrator
*
*/
public class ThreadPoolFactory implements FactoryBean<ThreadPoolExecutor>{
private ThreadPoolConfig config;
@Override
public ThreadPoolExecutor getObject() throws Exception {
ThreadPoolExecutor pool = null;
int corePoolSize = config.getCorePoolSize();
int maximumPoolSize = config.getMaximumPoolSize();
long keepAliveTime = config.getKeepAliveTime();
int queueSize = config.getQueueSize();
TimeUnit unit = config.getUnit();
ThreadFactory threadFactory = config.getThreadFactory();
RejectedExecutionHandler handler = config.getRejectedExecutionHandler();
BlockingQueue<Runnable> queue = config.getWorkQueue();
if(queue == null)
queue = new ArrayBlockingQueue<Runnable>(queueSize);
if(handler == null){
if (threadFactory == null)
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue);
else
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue, threadFactory);
}else{
if (threadFactory == null)
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, unit, queue, handler);
else
pool = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,keepAliveTime, unit, queue, threadFactory, handler);
}
return pool;
}
@Override
public Class<?> getObjectType() {
return ThreadPoolExecutor.class;
}
@Override
public boolean isSingleton() {
return true;
}
public ThreadPoolConfig getConfig() {
return config;
}
public void setConfig(ThreadPoolConfig config) {
this.config = config;
}
}
下面是集成配置到spring:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 任务队列配置-->
<bean id="workQueue" class="java.util.concurrent.ArrayBlockingQueue" >
<constructor-arg type="int" value="1000000" />
</bean>
<bean id="rejectedExecutionHandler" class="java.util.concurrent.ThreadPoolExecutor.AbortPolicy" />
<!-- 线程池基本配置-->
<bean id="threadPoolConfig" class="com.cloudsea.sys.compenent.threadpool.ThreadPoolConfig">
<property name="corePoolSize" value="100"/>
<property name="maximumPoolSize" value="2000"/>
<property name="keepAliveTime" value="5"/>
<property name="timeUnitName" value="MINUTES"/>
<property name="workQueue">
<ref bean="workQueue" />
</property>
<property name="rejectedExecutionHandler">
<ref bean="rejectedExecutionHandler" />
</property>
</bean>
<!-- 线程池创建工厂-->
<bean id="threadPoolFactory" class="com.cloudsea.sys.compenent.threadpool.ThreadPoolFactory">
<property name="config">
<ref bean="threadPoolConfig" />
</property>
</bean>
</beans>
下面是在编写的测试代码,直接在web系统中运行线程池,这里我就直接写在登陆代码中了
@Controller
public class LoginController {
@Autowired
ThreadPoolFactory threadPoolFactory;
@Resource(name = "writeFileProcess")
Process writeFileProcess;
@AccessRequired
@RequestMapping(value="/index", method=RequestMethod.GET)
public String index(User user) throws Exception{
ThreadPoolExecutor pool = threadPoolFactory.getObject();
final File file = new File("D:/1.txt");
if (file.exists())
file.delete();
file.createNewFile();
PrintWriter writer = new PrintWriter(new FileOutputStream(file), true);
for (int i = 0; i < 100000; i++){
Runnable runnable = new PrintThread(i, writer);
pool.execute(runnable);
}
writeFileProcess.write("OfficeStatus");
return "sys/user/login/login";
}
}
这个类定义线程运行任务,这里就是简单的把传过来的数据写到流中
class PrintThread implements Runnable{
int val;
PrintWriter writer;
public PrintThread(int val, PrintWriter writer){
this.val = val;
this.writer = writer;
}
public void run() {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
writer.println(val);
}
}
配置好后,启动web系统,登陆系统首页时,然后线程池就会启动,把1到100000的数据,用多线程并发的写到D:/1.txt中