最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例

 更新時(shí)間:2019年03月12日 11:00:50   作者:8blues  
今天小編就為大家分享一篇關(guān)于springmvc配置線程池Executor做多線程并發(fā)操作的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

加載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)鏈接

相關(guān)文章

最新評(píng)論

麻江县| 美姑县| 繁峙县| 高雄市| 马山县| 三明市| 习水县| 云龙县| 桐乡市| 望都县| 河间市| 深泽县| 德令哈市| 临沂市| 德化县| 彭州市| 邵武市| 盖州市| 拜城县| 洛浦县| 武威市| 阿瓦提县| 科尔| 轮台县| 贺兰县| 米林县| 内黄县| 苏尼特右旗| 榆树市| 吉木萨尔县| 柳州市| 休宁县| 安西县| 永吉县| 黄平县| 郓城县| 宣武区| 边坝县| 渝北区| 台安县| 云和县|