`
Tonyguxu
  • 浏览: 278568 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Concurrent-Executor Framework

 
阅读更多

Executor Framework

 

 

 

Callable  VS  Runnable

1.代表任务,会被线程执行。

不同

1. Callable型的任务能够返回结果或者抛出异常,而Runnable型任务不能

 

//java.util.concurrent 
//Interface Callable<V>
V call() throws Exception

//java.lang 
//Interface Runnable
void run()
 

2. 

 

两个问题

问题1.Thread的target是Runnable类型的,不能直接将Callable型对象作为Thread 的target。

如何将Callable型对象交给Thread?

 写道
Executor接口中的execute(Runnable command)方法的参数也是Runnable型,我们其实也想让Callable型
任务也能够被执行。根据这个需求注意在Executor的子接口ExecutorService中就增加了相关方法submit(Runnable/Callable),submit方法在子类AbstractExecutorService中给出了实现。实现里通过将Runnable/Callable型任务包装成RunnableFuture(继承了Runnable)从而可以交给execute(Runnable command)执行。

问题3:为什么不让FutureTask直接实现Runnable和Future两个接口,而是增加RunnableFuture接口
 

 

问题2.call()方法有返回值——但是call方法并不是直接被调用,而是作为线程执行体被调用。

如何获取call()的返回值?

 

为了解决问题2,JDK1.5中提供了Future接口来代表call()方法的返回值,并提供了一个实现类FutureTask。可以通过

get方法来获得最后执行的结果。

为了解决问题1,让FutureTask实现了Runnable接口,这样可以将 FutureTask交给Thread执行,间接将Callable交给了

Thread。


   

 

 


 

 

FutureTask<V>

 

A cancellable asynchronous computation.

可取消的异步计算 

This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation. 

 

The result can only be retrieved when the computation has completed; the get method will block if the computation has not yet completed. Once the computation has completed, the computation cannot be restarted or cancelled. 

 

A FutureTask can be used to wrap a Callable or Runnable object. Because FutureTask implements Runnable, a FutureTask can be submitted to an Executor for execution. 

 

In addition to serving as a standalone class, this class provides protected functionality that may be useful when creating customized task classes.

Executor接口

 

 

ExecutorService接口

 

 

AbstractExecutorService抽象类

 

 

 

 public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Object> ftask = newTaskFor(task, null);
        execute(ftask);//ftask是Runnable型
        return ftask;//ftask是Future型
    }

    public <T> Future<T> submit(Runnable task, T result) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task, result);
        execute(ftask);
        return ftask;
    }

    public <T> Future<T> submit(Callable<T> task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<T> ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }

protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
        return new FutureTask<T>(runnable, value);
    }

    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
        return new FutureTask<T>(callable);
    }

为什么不让FutureTask直接实现Runnable和Future两个接口,而是增加RunnableFuture接口?

注意submit方法有返回值Future,通过这个Future就可以对。。

 

ThreadPoolExecutor类

 

 

 

 

 

ScheduledExecutorService接口

 

 

ScheduledThreadPoolExecutor类

 

 

 

 

 

  • 大小: 630.5 KB
  • 大小: 15.3 KB
分享到:
评论

相关推荐

    spring-scheduling-sr:org.springframework.scheduling包源码阅读笔记,学习如何用好Java执行器(高级并发对象)-Spring Framework 3.2.10-源码包

    `ConcurrentTaskScheduler`是另一个`TaskScheduler`实现,它使用`java.util.concurrent.Executor`实例来执行任务。与`ThreadPoolTaskScheduler`不同,它不支持精确的定时任务,而是依赖于`Executor`的并发特性来处理...

    Java Concurrency Framework 的介绍

    ### Java Concurrency Framework 的介绍 #### 一、概述 本文档由 David Holmes 撰写,旨在为初学者提供一个关于 Java Concurrency Framework 的简单介绍。对于那些希望快速掌握 Java 并发编程基础概念的学习者来说...

    Spring Boot 线程池的创建、@Async 配置步骤及注意事项.docx

    &lt;bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt; &lt;!-- 配置核心线程数、最大线程数、队列容量等参数 --&gt; &lt;!-- application-context.xml --&gt; ``` ### 2. ...

    Java 9 Concurrency Cookbook - Second Edition

    Separate the thread management from the rest of the application with the Executor framework Solve problems using a parallelized version of the divide and conquer paradigm with the Fork / Join ...

    springmvc配置线程池Executor做多线程并发操作的代码实例

    &lt;bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt; &lt;!-- 核心线程数 --&gt; ${task.core_pool_size}" /&gt; &lt;!-- 最大线程数 --&gt; ${task.max_pool_size}" /&gt; &lt;!--...

    springboot中通过注解使用quartz

    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration @EnableScheduling @EnableAsync public ...

    spring定时器的mxl和注解配置

    &lt;bean id="taskExecutor" class="org.springframework.scheduled.concurrent.ThreadPoolTaskExecutor"&gt; &lt;!-- 这里可以注入需要的依赖 --&gt; &lt;task:executor id="executor" pool-size="5-10" /&gt; ...

    Java 7 Concurrency Cookbook

    It’s a special kind of executor oriented to execute tasks that will be divided into smaller ones using the divide and conquer technique. Chapter 6, Concurrent Collections will teach the readers to ...

    Sping 程序调度总结

    &lt;bean id="scheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean"&gt; ``` 4. **Quartz任务调度**: - 除了Spring内置的调度器,还可以...

    java时间触发器配置方法

    &lt;bean id="myScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"/&gt; ``` 然后,你可以创建一个带有`@Component`和`@Scheduled`注解的类来定义任务: ```java import org....

    定时任务启动多线线程示例

    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor() { ...

    spring3 配置quartz定时任务的使用

    &lt;property name="concurrent" value="false"/&gt; &lt;!-- Trigger 调度配置 --&gt; &lt;bean id="myTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"&gt; &lt;!-- 触发时间,单位为秒 --&gt; ...

    ESSH整合Spring 定时器配置

    &lt;bean id="myExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt; &lt;!-- 配置线程池相关参数 --&gt; &lt;bean id="myScheduler" class="org.springframework.scheduling.concurrent...

    Spring的定时容器案例

    &lt;bean id="myScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"&gt; ``` 6. **Cron表达式** 如果需要更复杂的定时规则,可以使用Cron表达式,它允许我们定义基于日期和...

    Spring quartz整合

    &lt;property name="concurrent" value="false" /&gt; ``` 这里,`jobExecutor`是包含`executeJob`方法的业务对象,`executeJob`是实际执行的任务逻辑。 在运行环境中,如MyEclipse 2016 1.0和JDK 1.8,这个小DEMO将...

    spring boot使用自定义配置的线程池执行Async异步任务

    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import com.chhliu.springboot.async.configuration.TaskThreadPoolConfig; @Configuration @EnableAsync public class ...

    ssm框架定时任务

    &lt;bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"&gt; &lt;bean id="scheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"&gt; ...

    Spring中实现定时调度的几种方法

    &lt;bean id="myExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"/&gt; &lt;bean id="myScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"/&gt; `...

Global site tag (gtag.js) - Google Analytics