springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例
加載xml文件
在ApplicationContext.xml文件里面添加
xmlns:task="http://www.springframework.org/schema/task"
xmlns文件并且xsi:schemaLocation中添加
在spring中配置Executor
在ApplicationContext.xml文件里面添加
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心線程數(shù) --> <property name="corePoolSize" value="${task.core_pool_size}" /> <!-- 最大線程數(shù) --> <property name="maxPoolSize" value="${task.max_pool_size}" /> <!-- 隊(duì)列最大長(zhǎng)度 --> <property name="queueCapacity" value="${task.queue_capacity}" /> <!-- 線程池維護(hù)線程所允許的空閑時(shí)間,默認(rèn)為60s --> <property name="keepAliveSeconds" value="${task.keep_alive_seconds}" /> </bean> <!-- 注解式 --> <task:annotation-driven />
在dbconfig.properties添加
maxOpenPreparedStatements=20 removeAbandoned=true removeAbandonedTimeout=1800 logAbandoned=true
這是分別對(duì)線程池做配置
添加依賴注入
在所需要的service或者controller類里面添加
@Resource(name = "taskExecutor") private TaskExecutor taskExecutor;
使用線程池進(jìn)行并發(fā)操作
代碼如下
taskExecutor.execute(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { //要進(jìn)行的并發(fā)操作 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } });
提示
注意在線程中操作變量時(shí)候變量的作用域范圍。需要在這個(gè)controller或者sevice中聲明變量如下
@Controller public class IndexController { int studentscount = 0; @RequestMapping(value = "/index.html") public ModelAndView goIndex() { logBefore(logger, "列表Center"); ModelAndView mv = this.getModelAndView(); taskExecutor.execute(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // 得到所有學(xué)生人數(shù) try { studentscount = coursesService.getStudentCount(pd); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); mv.addObject("studentscount", studentscount); mv.setViewName("common/index"); return mv; }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Spring Boot配置接口WebMvcConfigurer的實(shí)現(xiàn)
- SpringMVC+ZTree實(shí)現(xiàn)樹(shù)形菜單權(quán)限配置的方法
- spring mvc 讀取xml文件數(shù)據(jù)庫(kù)配置參數(shù)的方法
- 詳解IDEA用maven創(chuàng)建springMVC項(xiàng)目和配置
- Spring MVC配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的方法
- springmvc整合freemarker配置的詳細(xì)步驟
- SpringMVC集成redis配置的多種實(shí)現(xiàn)方法
相關(guān)文章
mybatis中如何實(shí)現(xiàn)一個(gè)標(biāo)簽執(zhí)行多個(gè)sql語(yǔ)句
這篇文章主要介紹了mybatis中如何實(shí)現(xiàn)一個(gè)標(biāo)簽執(zhí)行多個(gè)sql語(yǔ)句問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04JAVA發(fā)送http get/post請(qǐng)求,調(diào)用http接口、方法詳解
這篇文章主要介紹了Java發(fā)送http get/post請(qǐng)求調(diào)用接口/方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04java開(kāi)發(fā)中使用IDEA活動(dòng)模板快速增加注釋的方法
這篇文章主要介紹了java開(kāi)發(fā)中使用IDEA活動(dòng)模板快速增加注釋,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Spring框架學(xué)習(xí)筆記之方法注解@Bean的使用
這篇文章主要給大家介紹了關(guān)于Spring框架學(xué)習(xí)筆記之方法注解@Bean使用的相關(guān)資料,這是一個(gè)我們很常用的注解,作用是指示一個(gè)方法生成一個(gè)由Spring管理的Bean,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12