zoukankan      html  css  js  c++  java
  • 在spring中进行基于Executor的任务调度

    Executor

    java.util.concurrent.Executor接口的主要目的是要将“任务提交”和“任务执行”两者分离解耦。该接口定义了任务提交的方法,实现者可以提供不同的任务运行机制,解决具体的线程使用规则、调度方式等问题。

    Executor只有一个方法,即void execute(Runnable command) ,它接受任何实现了Runnable的实例,这个实例代表了一个需要执行的任务。

    Spring对Executor所提供的抽象

    Spring的org.springframework.core.task.TaskExecutor接口等同于java.util.concurrent.Executor接口。该接口和JDK 5.0的Executor接口拥相同的execute(Runnable task)方法。TaskExecutor拥有一个SchedulingTaskExecutor子接口,新增了任务调度规则定制的功能。

     

     Spring发行包中预定义TaskExecutor的实现

    • SyncTaskExecutor:

    位于org.springframework.core.task包中,实现了TaskExecutor接口。这个实现不会异步执行任务,相反,每次调用都在发起调用的主线程中执行。

    • SimpleAsyncTaskExecutor:

    位于org.springframework.core.task包中。这个实现没有使用线程池,在每次执行任务时都创建一个新线程。但是,它还是支持对并发总数设限,当超过线程并发总数限制时,阻塞新的任务直到有可用的资源。

    <bean id="simpleAsyncTaskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor">
    <property name="daemon" value="true" />
    <property name="concurrencyLimit" value="2" />
    <property name="threadNamePrefix" value="simpleAsyncTaskExecutor" />
    </bean>
    • ConcurrentTaskExecutor:

    位于org.springframework.scheduling.concurrent包中。该类是JDK 5.0的Executor的适配器,以便将JDK 5.0的Executor的当成Spring的TaskExecutor使用。

    <bean id="concurrentTaskExecutor"   class="org.springframework.scheduling.concurrent.ConcurrentTaskExecutor"/>
    • SimpleThreadPoolTaskExecutor:

    位于org.springframework.scheduling.quartz包中,这个类实际上是继承于Quartz的SimpleThreadPool类的子类,它将监听Spring的生命周期回调。当你有线程池,需要在Quartz和非Quartz组件中共用时,该类可以发挥它的用处。

    <bean id="simpleThreadPoolTaskExecutor" class="org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor">
    <property name="makeThreadsDaemons" value="true"/>
    <property name="threadCount" value="5" />
    <property name="threadNamePrefix" value="simpleThreadPoolTaskExecutor"/>
    <property name="waitForJobsToCompleteOnShutdown" value="true" />
    </bean>
    
     
    • ThreadPoolTaskExecutor:

    位于org.springframework.scheduling.concurrent包中。这个实现类只能在JDK 5.0中使用,它暴露的一些属性,方便在Spring中配置一个java.util.concurrent.ThreadPoolExecutor,并把它包装成TaskExecutor。

    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="2" />
    <property name="keepAliveSeconds" value="200" />
    <property name="maxPoolSize" value="10" />
    <property name="queueCapacity" value="60" />
    </bean>
    • TimerTaskExecutor:

    位于org.springframework.scheduling.timer包中。该类使用一个Timer作为其后台的实现。

    <bean id="timerTaskExecutor" class="org.springframework.scheduling.timer.TimerTaskExecutor">
    <property name="delay" value="10000" />
    </bean>
  • 相关阅读:
    数组有没有length()这个方法? String有没有length()这个方法?
    序列化接口的id有什么用?
    如何进行Hibernate的性能优化?
    构造器Constructor是否可被override?
    Collection框架中实现比较要实现什么接口?
    ArrayList如何实现插入的数据按自定义的方式有序存放?
    Java中会存在内存泄漏吗,请简单描述?
    List 和 Map 区别?
    面向对象的特征有哪些?
    垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?
  • 原文地址:https://www.cnblogs.com/wully/p/3433185.html
Copyright © 2011-2022 走看看