第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

springboot 多線程 獲取不到 service 空對象 無法執(zhí)行更新數(shù)據(jù)庫

springboot 多線程 獲取不到 service 空對象 無法執(zhí)行更新數(shù)據(jù)庫

猛跑小豬 2019-04-12 14:15:38
@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); 方法進行更新 求大佬幫助
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經(jīng)驗 獲得超2個贊

你的theadPool1 是@Component組件形式,也就是說這個實例是由spring容器進行維護的,它里面的對象也都是依賴spring進行實例化,但在你的public void threadPool() throws Exception {}這個方法里,你是通過new theadPool1(alist)的形式去實例化這個對象的,這樣其中的private IStockService stockService;對象是空值,正常情況下如果你執(zhí)行調(diào)用就會報空指針異常。解決辦法由public void threadPool() throws Exception {}這個方法所在的類去實例化類threadPool,也就是通過@Autowired去實例化

查看完整回答
反對 回復 2019-05-14
?
富國滬深

TA貢獻1790條經(jīng)驗 獲得超9個贊

這邊 我后續(xù) 采用了兩種解決方法, 一種 就是直接從 springboot 的上下文中 去獲取實例, 二就是直接將這個service 對象 直接通過傳參的方式進行傳遞,

查看完整回答
反對 回復 2019-05-14
  • 2 回答
  • 0 關(guān)注
  • 1725 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號