java springBoot启动即执行多线程处理业务

package com.ann.demo.job;

import com.ann.demo.primaryEntity.ScanningFile;
import com.ann.demo.primaryRepository.ScanningFileRepository;
import com.ann.demo.service.AnalysisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2021/12/22 0:07
 * @UpdateUser: 曾文和
 * @UpdateDate: 2021/12/22 0:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Component
public class DataRunner implements ApplicationRunner {
    static final Logger logger = LoggerFactory.getLogger(DataRunner.class);
    private static int flag = 0;
    private static int count = 0;
    @Autowired
    private ScanningFileRepository scanningFileRepository;

    @Autowired
    private AnalysisService analysisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if(flag == 0) {
            List<ScanningFile> scanningFiles = scanningFileRepository.findScanningFileList();
            count = scanningFiles.size();
            logger.error("查询出待处理记录数:"+scanningFiles.size()+"个");
            analysisService.handleScanningFile(scanningFiles);
            flag = 1;
        }
        if(count != 0) {
            Thread t1 = new Thread1();
            Thread t2 = new Thread2();
            Thread t3 = new Thread3();
            Thread t4 = new Thread4();
            Thread t5 = new Thread5();
            Thread t6 = new Thread6();
            Thread t7 = new Thread7();
            Thread t8 = new Thread8();
            Thread t9 = new Thread9();
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
            t6.start();
            t7.start();
            t8.start();
            t9.start();
        }
    }

    class Thread1 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread2 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread3 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread4 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread5 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread6 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
    class Thread7 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread8 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread9 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    class Thread10 extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    count--;
                    analysisService.startThread(count);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值