spirng线程池的配置与使用

1、在xml中配置线程池

<!-- 配置线程池 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  <!-- 核心线程数 -->
  <property name="corePoolSize" value="4" />
  <!-- 最大线程数 -->
  <property name="maxPoolSize" value="4" />
  <!-- 队列最大长度 -->
  <property name="queueCapacity" value="1000" />
  <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
  <property name="keepAliveSeconds" value="60" />
</bean>
<!-- 注解式 -->
<task:annotation-driven />

2,定义任务类,并循环执行

package com.mediaforce.crawl.quarz;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

@Component
public class SolrWarning {

    @Resource(name = "taskExecutor")
    private TaskExecutor taskExecutor;

    public void warning() {
        try {
            for (int i = 0; i < 10; i++) {
                MyTask task = new MyTask(i);
                taskExecutor.execute(task);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final String s = "风萧萧兮易水寒,壮士一去兮不复还";

    public void run() {
        System.out.println(sdf.format(new Date())
                    + Thread.currentThread().getName() + "senti:"
                    + ToAnalysis.parse(s));
        System.out.println("num:" + taskNum);
    }
}

3、关于线程池的详解,请参考

http://www.cnblogs.com/dolphin0520/p/3932921.html

上一篇:【Shared Server Mode】测试调整shared_servers参数对数据库的影响


下一篇:自解压的方式创建VC++程序的打包