zoukankan      html  css  js  c++  java
  • 简单线程池开启线程

    线程池:

    serviceimpl 层进行调用:

    ArrayBlockingQueue<Runnable> arrayWorkQueue = new ArrayBlockingQueue(50000);
    ExecutorService threadPool = new ThreadPoolExecutor(
    10, //corePoolSize线程池中核心线程数
    50, //maximumPoolSize 线程池中最大线程数
    60, //线程池中线程的最大空闲时间,超过这个时间空闲线程将被回收
    TimeUnit.SECONDS,//时间单位
    arrayWorkQueue,
    new ThreadPoolExecutor.DiscardOldestPolicy()//如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)
    );

    /**
    *这里也可以写一些操作,比如说对数据的加减乘除
    *
    */
     //每1000条开启一条线程
    if (list.size() >= 1000) {
    List<TBalances> list1 = new ArrayList();
    list1.addAll(list);
    threadPool.execute(new BatchThread(list1));
    list = new ArrayList<TBalances>();
    }

    }

    if (list.size() > 0) {
    threadPool.execute(new BatchThread(list));
    }

    util 类:
    开启的线程类(实际线程操作):
    import cn.hc.erp.base.model.TBalances;
    import cn.hc.erp.base.service.DetailService;

    import java.util.List;

    public class BatchThread implements Runnable {

    private DetailService detailService = SpringUtil.getBean("detailService", DetailService.class);

    private List<TBalances> list;

    public BatchThread(List<TBalances> list) {
    this.list = list;
    }

    public BatchThread() {

    }


    @Override
    public void run() {
    System.out.println(Thread.currentThread().getName() + "正在执行");
    detailService.updateBatch(this.list);
    detailService.batchInsert(this.list);
    //为了减缓数据库压力,人工干预延迟5秒释放线程资源
    try {
    Thread.currentThread().sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }

    }
    获取detailService的SpringUtil类:


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

    @Service("springContextUtil")
    public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext arg0)
    throws BeansException {
    SpringUtil.applicationContext = arg0;

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

    public static <T> T getBean(String name,Class<T> clazz){
    return applicationContext.getBean(name,clazz);
    }


    public static <T> T getBean(Class<T> clazz){
    return applicationContext.getBean(clazz);
    }
    }
    为了不让子线程随着主线程结束:
    Scanner scanner = new Scanner(System.in);
    System.out.println("********" + scanner.nextLine());



  • 相关阅读:
    scp命令报错-bash: scp: command not found
    shell比较两个字符串是否相等
    bat脚本:自动压缩n天前的文件【转载】
    shell bash判断文件或文件夹是否存在
    linux文件分割(将大的日志文件分割成小的)【转载】
    TCP/IP模型各个层次的功能和协议
    nginx初级安装配置
    Heartbeat+DRBD+MySQL高可用方案【转】
    【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
    oracle的exp和imp命令的使用【转载】
  • 原文地址:https://www.cnblogs.com/Dream--/p/8477768.html
Copyright © 2011-2022 走看看