欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Spring中的線程池和定時任務(wù)功能解析

 更新時間:2017年09月13日 08:44:45   作者:HelloWorld1992  
下面小編就為大家?guī)硪黄赟pring中的線程池和定時任務(wù)功能解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1.功能介紹

Spring框架提供了線程池和定時任務(wù)執(zhí)行的抽象接口:TaskExecutor和TaskScheduler來支持異步執(zhí)行任務(wù)和定時執(zhí)行任務(wù)功能。同時使用框架自己定義的抽象接口來屏蔽掉底層JDK版本間以及Java EE中的線程池和定時任務(wù)處理的差異。
另外Spring還支持集成JDK內(nèi)部的定時器Timer和Quartz Scheduler框架。

2.線程池的抽象:TaskExecutor

TaskExecutor涉及到的相關(guān)類圖如下:



TaskExecutor接口源代碼如下所示:

public interface TaskExecutor extends Executor {

 /**
  * Execute the given {@code task}.
  * <p>The call might return immediately if the implementation uses
  * an asynchronous execution strategy, or might block in the case
  * of synchronous execution.
  * @param task the {@code Runnable} to execute (never {@code null})
  * @throws TaskRejectedException if the given task was not accepted
  */
 @Override
 void execute(Runnable task);

}

此接口和Executor幾乎完全一樣,只定義了一個接收Runnable參數(shù)的方法,據(jù)Spring官方介紹此接口最初是為了在其他組建中使用線程時,將JKD抽離出來而設(shè)計的。在Spring的一些其他組件中比如ApplicationEventMulticaster,Quartz都是使用TaskExecutor來作為線程池的抽象的。

3.Spring提供的TaskExecutor的實現(xiàn)類

org.springframework.core.task.SimpleAsyncTaskExecutor

此實現(xiàn)支持任務(wù)的異步執(zhí)行,但是此實現(xiàn)沒有線程的復(fù)用,每次執(zhí)行一個提交的任務(wù)時候都會新建一個線程,任務(wù)執(zhí)行完成后會將線程關(guān)閉,最大并發(fā)數(shù)默認是沒有限制的,但是可以通過調(diào)用下面的方法來設(shè)置最大并發(fā)數(shù)。一般使用線程池來代替此實現(xiàn),特別是執(zhí)行一些生命周期很短的任務(wù)的時候。

public void setConcurrencyLimit(int concurrencyLimit) {
  this.concurrencyThrottle.setConcurrencyLimit(concurrencyLimit);
 }

Spring還提供了同步任務(wù)執(zhí)行的實現(xiàn)類:

 org.springframework.core.task.SyncTaskExecutor

此類中只有一個方法,代碼如下:

@Override
 public void execute(Runnable task) {
  Assert.notNull(task, "Runnable must not be null");
  task.run();
 }

此方法中直接調(diào)用傳入的Runable對象的run方法,因此在執(zhí)行此方法的時候不會另外開啟新的線程,只是普通的方法調(diào)用,同步執(zhí)行提交的Runable對象。

Spring有兩個線程池的實現(xiàn)類,分別為:SimpleThreadPoolTaskExecutor和ThreadPoolTaskExecutor,其中當我們有Quarts和非Quarts共享同一個線程池的需求的時候使用SimpleThreadPoolTaskExecutor,除了這種情況,我們一般是使用
ThreadPoolTaskExecutor,此實現(xiàn)可以通過屬性注入來配置線程池的相關(guān)配置。 ThreadPoolTaskExecutor中屬性注入的源碼如下:此配置可以在運行期修改,代碼中修改過程使用了同步控制。

/**
 * Set the ThreadPoolExecutor's core pool size.
 * Default is 1.
 * <p><b>This setting can be modified at runtime, for example through JMX.</b>
 */
public void setCorePoolSize(int corePoolSize) {
 synchronized (this.poolSizeMonitor) {
  this.corePoolSize = corePoolSize;
  if (this.threadPoolExecutor != null) {
   this.threadPoolExecutor.setCorePoolSize(corePoolSize);
  }
 }
}

4.TaskExecutor使用Demo

首先定義一個任務(wù)如下所示:

public class DataSimulation implements Runnable{

 private HourAverageValueDao hourAverageValueDao;

 @Override
 public void run() {

  Random random = new Random();
  AverageValue averageValue = new AverageValue();
  averageValue.setAverageVaule(random.nextInt(100));
  averageValue.setCreatedTime(new Date());
  hourAverageValueDao.insert(averageValue);

 }
}

此任務(wù)中產(chǎn)生一個隨機數(shù),并封裝成一個類對象,并將此數(shù)據(jù)插入到數(shù)據(jù)庫中。

然后需要定一個類,使用TaskExecutor,代碼如下:

public class DataFacotory {

 private TaskExecutor executor;

 public TaskExecutor getExecutor() {
  return executor;
 }

 public void setExecutor(TaskExecutor executor) {
  this.executor = executor;
 }

 public void dataFactory(){

  for (int i =0; i < 10; i++){
   executor.execute(new DataSimulation());
  }
 }
}

此類中定義了TaskExecutor的屬性,并定一個方法,此方法中提交10個任務(wù)到TaskExecutor,下面只需配置Spring文件,注入TaskExecutor就可以實現(xiàn)線程池的使用。配置文件如下所示:

<bean id = "taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  <property name="corePoolSize" value = "5"></property>
  <property name = "maxPoolSize" value="10"></property>
  <property name="queueCapacity" value="25"></property>
 </bean>

完成配置后即可使用此線程池。Spring提供的線程池可以通過配置文件配置線程池的配置,相比JDk自帶的線程池是一個很大的優(yōu)勢。

5.為什么使用線程池

1.通過使用線程池來實現(xiàn)線程的復(fù)用,減少線程創(chuàng)建和銷毀的開銷

2.將執(zhí)行線程的任務(wù)交給線程池來操作,一定意義上實現(xiàn)了解耦

3.使用線程池可以控制任務(wù)的最大并發(fā)數(shù)目,這個在防止內(nèi)存溢出以及并發(fā)優(yōu)化方面有很重要的作用。

6.定時任務(wù)抽象類:TaskScheduler

TaskScheduler接口源代碼如下:

public interface TaskScheduler {
 //通過觸發(fā)器來決定task是否執(zhí)行
ScheduledFuture schedule(Runnable task, Trigger trigger);
//在starttime的時候執(zhí)行一次
ScheduledFuture schedule(Runnable task, Date startTime);
從starttime開始每個period時間段執(zhí)行一次task
ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period);
每隔period執(zhí)行一次
ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
從startTime開始每隔delay長時間執(zhí)行一次
ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
每隔delay時間執(zhí)行一次
ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay);
}

指定開始時間的接口,如果時間已經(jīng)是過去的某一時間點,則此任務(wù)會馬上執(zhí)行一次。以上幾種執(zhí)行方式傳入Trigger的方式是用的最多的,Trigger接口中只定義了一個方法:

Date nextExecutionTime(TriggerContext triggerContext);

其中參數(shù)類型TriggerContext的定義如下:

public interface TriggerContext {

/**
 * Return the last <i>scheduled</i> execution time of the task,
 * or {@code null} if not scheduled before.
 */
Date lastScheduledExecutionTime();

/**
 * Return the last <i>actual</i> execution time of the task,
 * or {@code null} if not scheduled before.
 */
Date lastActualExecutionTime();

/**
 * Return the last completion time of the task,
 * or {@code null} if not scheduled before.
 */
Date lastCompletionTime();

}

提供了獲取上一次任務(wù)執(zhí)行信息的接口。我們通過實現(xiàn)Trigger接口可以實現(xiàn)自定義觸發(fā)器來執(zhí)行執(zhí)行task。當然Spring也提供了兩個默認的實現(xiàn)類:PeriodicTrigger和CronTrigger。

7.TaskScheduler定時任務(wù)Demo

首先在Spring配置文件中啟用注解配置如下:

<task:annotation-driven scheduler="myScheduler"/> //指定scheduler屬性是可選項,不添加也可以正常使用
<task:scheduler id="myScheduler" pool-size="10"/>

然后創(chuàng)建service,并在service中使用@Scheduled注解創(chuàng)建定時任務(wù),代碼如下:

@Component
public class SchedulerPoolTest {

 @Scheduled(cron = "0 * * * * ?")
 public void task1(){
  System.out.println("test");
  Thread thread = Thread.currentThread();
  System.out.println("ThreadName:" + thread.getName() + ",id:" + thread.getId() + ",group:" + thread.getThreadGroup());

 }

 @Scheduled(fixedDelay = 5000)
 public void task2(){
  System.out.println("test");
  Thread thread = Thread.currentThread();
  System.out.println("ThreadName:" + thread.getName() + ",id:" + thread.getId() + ",group:" + thread.getThreadGroup());

 }

}

只是添加以上內(nèi)容可能還不能正常執(zhí)行task,還需要注意以下兩點:

1.必須將SchedulerPoolTest類包含在spring所掃描的包里面

配置如下:

<context:component-scan base-package="com.zjut.task" />

2.需要在web.xml中添加spring配置文件的監(jiān)聽器,代碼如下:

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath*:spring-task.xml</param-value>
 </context-param>

 <listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

添加以上內(nèi)容后,啟動服務(wù)器,將會定時執(zhí)行任務(wù)。

8.Cron表達式

Cron表達式由6個字符串組成,每個字符串分別代表:

{秒} {分} {時} {日} {月} {周}

其中每個字符串所允許的取值范圍為:

字段名     允許的值      允許的特殊字符 
秒     0-59       , - * / 
分     0-59       , - * / 
小時     0-23       , - * / 
日     1-31       , - * ? / L W C 
月     1-12 or JAN-DEC     , - * / 
周幾     1-7 or SUN-SAT     , - * ? / L C #

常用的Cron表達式:

"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44觸發(fā)
"0 0/5 14,18 * * ?" 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發(fā)
"15-30/5 * * * * ?" 每分鐘的15秒到30秒之間開始觸發(fā),每隔5秒觸發(fā)一次
"0 15 10 ? * 5#3" 每個月第三周的星期四的10點15分0秒觸發(fā)任務(wù)

注:問號是用于避免日和周的設(shè)定由沖突而用的,當其中一個設(shè)置了具體的值,另外一個必須使用?。另外推薦一個Cron表達式生成的鏈接:http://www.cronmaker.com/

9.@Async注解

Async注解提供了異步調(diào)用方法的功能,當調(diào)用由此注解的方法的時候方法調(diào)用者會馬上返回而不會等待調(diào)用的方法執(zhí)行完成,被調(diào)用的方法會從線程池中分配一個線程來執(zhí)行此方法。

10.Spring定時任務(wù)中并發(fā)執(zhí)行的問題

同一個任務(wù),當上一個任務(wù)沒有執(zhí)行完成的時候,新的任務(wù)不會執(zhí)行。

不同任務(wù)的情況下:TODO...

以上這篇基于Spring中的線程池和定時任務(wù)功能解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié))

    玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié))

    這篇文章主要介紹了玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Oracle+Mybatis的foreach insert批量插入報錯的快速解決辦法

    Oracle+Mybatis的foreach insert批量插入報錯的快速解決辦法

    本文給大家介紹Oracle+Mybatis的foreach insert批量插入報錯的快速解決辦法,非常不錯,具有參考借鑒價值,感興趣的朋友參考下吧
    2016-08-08
  • Java8實現(xiàn)Stream流的合并的方法展示

    Java8實現(xiàn)Stream流的合并的方法展示

    本文介紹了Java8中Stream流的合并方法,包括concat()、flatMap()和reduce()三種方法。其中,concat()方法可以將兩個Stream流合并成一個,flatMap()方法可以將一個Stream流中的元素映射成多個Stream流并合并成一個,reduce()方法可以將Stream流中的元素逐個合并成一個結(jié)果
    2023-05-05
  • SpringBoot訪問windows共享文件的方法

    SpringBoot訪問windows共享文件的方法

    這篇文章主要介紹了SpringBoot訪問windows共享文件,項目使用minio存儲且不在同一臺服務(wù)器上,為了優(yōu)化速度決定使用windows共享功能進行文件傳輸,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • java中的export方法實現(xiàn)導(dǎo)出excel文件

    java中的export方法實現(xiàn)導(dǎo)出excel文件

    這篇文章主要介紹了java中的export方法實現(xiàn)導(dǎo)出excel文件,文章圍繞java導(dǎo)出excel文件的相關(guān)資料展開詳細內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • springboot對象為null的屬性在json中不顯示的解決

    springboot對象為null的屬性在json中不顯示的解決

    這篇文章主要介紹了springboot對象為null的屬性在json中不顯示的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot如何動態(tài)修改Scheduled(系統(tǒng)啟動默認執(zhí)行,動態(tài)修改)

    SpringBoot如何動態(tài)修改Scheduled(系統(tǒng)啟動默認執(zhí)行,動態(tài)修改)

    這篇文章主要介紹了SpringBoot如何動態(tài)修改Scheduled(系統(tǒng)啟動默認執(zhí)行,動態(tài)修改)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java反射機制詳解_動力節(jié)點Java學院整理

    Java反射機制詳解_動力節(jié)點Java學院整理

    Java 反射機制。通俗來講呢,就是在運行狀態(tài)中,我們可以根據(jù)“類的部分已經(jīng)的信息”來還原“類的全部的信息”。這篇文章給大家詳細介紹了java反射機制的知識,感興趣的朋友一起看看吧
    2017-06-06
  • 詳解Java單例模式中的餓漢和懶漢模式

    詳解Java單例模式中的餓漢和懶漢模式

    這篇文章主要介紹了詳解Java單例模式中的餓漢和懶漢模式,單例模式中有兩種模式一種是餓漢模式,一種是懶漢模式,那么他們有什么區(qū)別呢,需要的朋友可以參考下本文
    2023-04-04
  • 詳解Elasticsearch如何把一個索引變?yōu)橹蛔x

    詳解Elasticsearch如何把一個索引變?yōu)橹蛔x

    這篇文章主要為大家介紹了詳解Elasticsearch如何把一個索引變?yōu)橹蛔x示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論