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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

猛跑小豬 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<>();         //切分任務(wù)         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("當(dāng)前運(yùn)行的線程名稱(chēng):" + Thread.currentThread().getName());         stockService.uptStock(stockBeans);         System.out.println("當(dāng)前運(yùn)行的線程ID:" + Thread.currentThread().getId());         return stockBeans;無(wú)法進(jìn)入  stockService.uptStock(stockBeans); 方法進(jìn)行更新 求大佬幫助
查看完整描述

2 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

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

查看完整回答
反對(duì) 回復(fù) 2019-05-14
?
富國(guó)滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

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

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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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