Java队列——线程池创建的例子

  线程池为线程生命周期开销问题和资源不足问题提供了解决方案。通过对多个任务重用线程,线程创建的开销被分摊到了多个任务上。其好处是,因为在请求到达时线程已经存在,所以无意中也消除了线程创建所带来的延迟。这样,就可以立即为请求服务,使应用程序响应更快。而且,通过适当地调整线程池中的线程数目,也就是当请求的数目超过某个阈值时,就强制其它任何新到的请求一直等待,直到获得一个线程来处理为止,从而可以防止资源不足。

 

具体的线程池详细见解 如: http://www.importnew.com/19011.html

 

使用spring管理线程池的使用

1、创建线程池的配置信息threads.properties

####业务线程池配置####
#是否启用自定义线程池。true时启动,以下参数生效
handler.threads.custom=false
#核心线程数
handler.threads.corePoolSize=20
#最大线程数
handler.threads.maximumPoolSize=1000
#空闲线程存活时间,单位秒
handler.threads.keepAliveTime=100
#工作队列大小,为0是无限大
handler.threads.workQueue=0

 

 

2、创建 线程池 配置,ThreadsPoolConfig.java

package com.hk.core.concurrent;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 线程池 配置
 */
@Component
public class ThreadsPoolConfig {

    /**
     * 是否开启自定义线程池
     */
    @Value("${handler.threads.custom}")
    private boolean custom;
    /**
     * 核心线程数
     */
    @Value("${handler.threads.corePoolSize}")
    private int corePoolSize;
    /**
     * 线程池最大线程数
     */
    @Value("${handler.threads.maximumPoolSize}")
    private int maximumPoolSize;
    /**
     * 空闲线程存活时间(对核心线程无效)
     */
    @Value("${handler.threads.keepAliveTime}")
    private long keepAliveTime;
    /**
     * 任务队列大小,0时为无界队列
     */
    @Value("${handler.threads.workQueue}")
    private int workQueue;

    public boolean isCustom() {
        return custom;
    }

    public void setCustom(boolean custom) {
        this.custom = custom;
    }

    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 int getWorkQueue() {
        return workQueue;
    }

    public void setWorkQueue(int workQueue) {
        this.workQueue = workQueue;
    }
    
}

 

 

3、创建 线程池 处理器管理线程 HandlerThreadsPool.java

package com.hk.core.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.PreDestroy;

/**
 * 线程管理器
 */
public class HandlerThreadsPool {

    public ExecutorService executorService;
    
    public HandlerThreadsPool() {
        // TODO Auto-generated constructor stub
        this.executorService=Executors.newCachedThreadPool();
    }
    
    public HandlerThreadsPool(ThreadsPoolConfig config) {
        // TODO Auto-generated constructor stub
        if(config.isCustom()){
            BlockingQueue<Runnable> queue=null;
            if(config.getWorkQueue()>0){
                queue=new LinkedBlockingQueue<Runnable>(config.getWorkQueue()); // 一般使用 LinkedBlockingQueue 队列
            }else{
                queue=new LinkedBlockingQueue<Runnable>();
            }
        // 配置线程池信息
this.executorService=new ThreadPoolExecutor( config.getCorePoolSize(), config.getMaximumPoolSize(), config.getKeepAliveTime(), TimeUnit.SECONDS, queue, new ThreadPoolExecutor.AbortPolicy()//拒绝策略,任务队列满后,新的任务将被丢弃,并抛出异常 ); }else{ this.executorService=Executors.newCachedThreadPool(); } }

/*
   * 创建线程,对线程处理事件
*/
public void execute(Runnable runnable){ executorService.execute(runnable); } /*
   * 对象销毁时,销毁线程
   */
@PreDestroy
public void stop() { executorService.shutdown(); } }

 

 

4、使用线程池

package com.hk.core.concurrent;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsgHandler implements Runnable{

    
    @Autowired
    private  ThreadsPoolConfig config;  // 注入 配置
    
    @Override
    public void run() {
       // do 这里 写 处理的逻辑
        System.out.println("创建线程 处理事务....");
    }

    
    @PostConstruct
    public void loadThreadsPool(){
        
        // 初始化 线程池
        HandlerThreadsPool handlerThreadsPool=new HandlerThreadsPool(config); 
        
        //调用线程池,创建线程  。处理事件
        handlerThreadsPool.execute(new MsgHandler());
    }
}

简单的例子就这样完美使用线程池了

posted @ 2017-11-22 14:26  低调人生  阅读(5250)  评论(1编辑  收藏  举报