有一個(gè)使用場(chǎng)景,在用戶一次的訪問(wèn)中,我向steam的dota2接口請(qǐng)求了20條用戶的最近比賽記錄,然后傳到j(luò)sp渲染了,我想把這20條記錄存在數(shù)據(jù)庫(kù)中,這個(gè)操作比較耗時(shí),就新開(kāi)了一個(gè)線程這樣做會(huì)不會(huì)導(dǎo)致線程安全問(wèn)題,好像不推薦在servlet中新開(kāi)線程?還有其他的解決方案嗎?模擬代碼@RequestMapping("/thread")@ResponseBodypublicStringactionThread(){Threadt=newThread(newRunnable(){@Overridepublicvoidrun(){try{Thread.sleep(5000);System.out.println("數(shù)據(jù)庫(kù)操作");}catch(InterruptedExceptione){e.printStackTrace();}}});t.start();return"nihao";}謝謝各位查了文檔用了springmvc容器級(jí)的taskexecutor,可以限制線程數(shù)目和等待隊(duì)列的大小。@Asyncpublicvoidtest(Strings){try{Thread.currentThread().sleep(5000);System.out.println("testasny"+s);}catch(InterruptedExceptione){e.printStackTrace();}}
springmvc controller 請(qǐng)求中新開(kāi)線程的困惑
紫衣仙女
2019-04-13 08:37:20