创建线程池

用的单例懒单例创建的缓存线程池


package util.sup;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolManager {

public final static ThreadPoolManager I = new ThreadPoolManager();

private ThreadPoolExecutor threadPoolExecutor;

public ThreadPoolExecutor getThreadPoolExecutor() {
    return threadPoolExecutor;
}

public ThreadPoolManager() {
    init();
}

private void init() {
    construteThreadPool();
}

// 获取单例的线程池对象
private void construteThreadPool() {
    if (this.threadPoolExecutor == null) {
        synchronized (ThreadPoolManager.class) {
            if (this.threadPoolExecutor == null) {
                this.threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, 
                    TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
            }
        }
    }
}

}


使用的时候直接
ThreadPoolManager.I.getThreadPoolExecutor().execute(new InsertSmsDayTimeLineTask(null));

其中InsertSmsDayTimeLineTask就是你要跑的线程类了,当然构造函数自己随意了

package stock.simulator.trader.web.task;

import java.util.List;

import stock.simulator.trader.web.data.SmsDayTimeLineDO;
import util.sup.ServiceBean;

public class InsertSmsDayTimeLineTask extends ServiceBean implements Runnable {

private List<SmsDayTimeLineDO> smsDayTimeLineList;

public InsertSmsDayTimeLineTask(List<SmsDayTimeLineDO> smsDayTimeLineList) {
    this.smsDayTimeLineList = smsDayTimeLineList;
}

@Override
public void run() {
    // 你想干啥
}

}


ServiceBean 是用来做service引用的

package util.sup;

import stock.simulator.trader.web.ApplicationContextUtil;
import stock.simulator.trader.web.service.SmsMarketQuotationService;
import stock.simulator.trader.web.service.StockService;

public class ServiceBean {

public StockService getStockService() {
    return (StockService) ApplicationContextUtil.getBean("stockService");
}

public SmsMarketQuotationService getSmsMarketQuotationService() {
    return (SmsMarketQuotationService) ApplicationContextUtil.getBean("smsMarketQuotationService");
}

}


package stock.simulator.trader.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import domain.model.data.StockPreloadingManager;

/**
* Created by dzd-technology01 on 2017/9/25.
*/
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;

public static ApplicationContext getApplicationContext() {
    return applicationContext;
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    ApplicationContextUtil.applicationContext = applicationContext;

    StockPreloadingManager.I.init();
}

public static Object getBean(String beanName) {
    return applicationContext.getBean(beanName);
}

/**
 * 通过class获取Bean.
 */
public static <T> T getBean(Class<T> clazz) {
    return getApplicationContext().getBean(clazz);
}

/**
 * 通过name,以及Clazz返回指定的Bean
 * 
 * @param name
 * @param clazz
 * @param <T>
 * @return
 */
public static <T> T getBean(String name, Class<T> clazz) {
    return getApplicationContext().getBean(name, clazz);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值