smile06211114
chenlianxiang_Blog
2019-03-28 18:32

springboot 多线程 获取不到 service 空对象 无法执行更新数据库

5
  • spring
  • java
 @Bean
    public ExecutorService executorService(){
        ExecutorService executorService=Executors.newFixedThreadPool(7);
        return executorService;
    }


          @RequestMapping(value = "threadPool")
    public void threadPool() throws Exception {
        int size=6;

        List<stockBean> stockBeans = stockService.queryStock();
        List<stockBean> retInfoList2 = new ArrayList<stockBean>();
//        ExecutorService executorService=Executors.newFixedThreadPool(10);
        List<List<stockBean>> subTaskList = new ArrayList<List<stockBean>>();

        List<FutureTask<List<stockBean>>> futureList=new ArrayList<>();

        //切分任务
        for (int i =0 ;i<=stockBeans.size();i=i+size) {

            subTaskList.add(stockBeans.subList(i, Math.min(i+size, stockBeans.size())));
        }
        for (List<stockBean> alist : subTaskList) {

            FutureTask<List<stockBean>> task = new FutureTask<List<stockBean>>(new theadPool1(alist));
            executorService.execute(task);
            futureList.add(task);

        }

        for (FutureTask<List<stockBean>> futureTask : futureList) {

            retInfoList2.addAll(futureTask.get());
        }

    }



        @Component
public class theadPool1 implements Callable<List<stockBean>> {

    @Autowired
    private IStockService stockService;



    private List<stockBean> stockBeans=null;
    public theadPool1(List<stockBean> stockBeans){
        this.stockBeans=stockBeans;
    }
    @Override
    public List<stockBean> call() throws Exception {
        System.out.println("当前运行的线程名称:" + Thread.currentThread().getName());
        stockService.uptStock(stockBeans);
        System.out.println("当前运行的线程ID:" + Thread.currentThread().getId());
        return stockBeans;

无法进入  stockService.uptStock(stockBeans); 方法进行更新

求大佬帮助
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答