【初识】-JUC·Executor框架

前言

多线程和并发这两个东西真的是向往已久,老是有一种神秘的感受,想去探索一波,又担忧水平不够没法驾驭。想以读书笔记的方式来写,可是又以为缺乏本身的一些思考;可是在没有足够并发编程经验的状况下又无法去写出很深入的东西,毕竟没有踩过坑。因此在阅读spring源码的同时,也想抽点时间来看一看JUC的东西,关于这块只能说是记录本身学习JUC的一个过程,尝试用一些具体的代码demo来加深理解。因此就把本系列写成《【 初识】-JUC·XXXX》,用来让本身打开并发编程的大门。java

JUC

JUC即java.util.concurrent;也就是java提供的并发包。JUC中从包结构上来看主要是:spring

  • java.util.concurrent编程

    在这个包下面主要是线程池、并发集合以及一些并发工具类。线程池相关是围绕Excetor框架来构建;这也是本文下面部分的重点。多线程

  • java.util.concurrent.atomic并发

    这个包下面是一些原子操做类,算是并发辅助工具类,基本实现依赖于CAS;框架

  • java.util.concurrent.locks异步

    这个从名字就能够知道它的做用,就是提供锁。工具

JUC各个模块的类

  • 总体框架

  • atomic

  • locks

  • 并发集合

  • 并发工具

  • forkJoin学习

    fork-join在JUC中有下面三个类:this

    public class ForkJoinPool extends AbstractExecutorService 复制代码
    public abstract class ForkJoinTask<V> implements Future<V>, Serializable 复制代码
    public class ForkJoinWorkerThread extends Thread 复制代码

Future

Future提供了能够获取异步执行结果的方法,区别于Runnable的run方法,run是不提供返回结果的。

public interface Future<V> {
    //取消
    boolean cancel(boolean mayInterruptIfRunning);
    //若是任务完成前被取消,则返回true。
    boolean isCancelled();
    //若是任务执行结束,不管是正常结束或是中途取消仍是发生异常,都返回true。
    boolean isDone();
    //获取异步执行的结果,若是没有结果可用,此方法会阻塞直到异步计算完成。
    V get() throws InterruptedException, ExecutionException;
    //获取异步执行结果,若是没有结果可用,此方法会阻塞,可是会有时间限制,
    //若是阻塞时间超过设定的timeout时间,该方法将抛出异常。
    V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
复制代码

Callable

声明了一个名称为call()的方法,同时这个方法能够有返回值V,也能够抛出异常

public interface Callable<V> { 
  V call() throws Exception; 
} 
复制代码

关于Callable和Future的使用通常状况下都是结合咱们的线程池来使用的。

Executor

Executor接口是线程池实现的顶级接口,其和spring中的BeanFactory所承担的角色差很少,就是提供顶级的功能约束,具体实现交于不一样子类来完成。

public interface Executor {
    /** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the <tt>Executor</tt> implementation. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution. * @throws NullPointerException if command is null */
    void execute(Runnable command);
}
复制代码

下面是JUC中Executor框架的总体结构:

ExecutorService

public interface ExecutorService extends Executor {
    //关闭线程池
    void shutdown();
    
    List<Runnable> shutdownNow();
    //是否为Shutdown状态
    boolean isShutdown();
    //是否为Terminated状态
    boolean isTerminated();
    
    //超过超时时间时,会监测ExecutorService是否已经关闭
    //若关闭则返回true,不然返回false。
    //通常状况下会和shutdown方法组合使用。
    boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
    
    //返回一个Future对象,参数接收的是一个Callable的实现
    //Callable接口中的call()方法有一个返回值,能够返回任务的执行结果
    //区别于Runnable接口中的run()方法(void修饰,没有返回值)。
    <T> Future<T> submit(Callable<T> task);
    
    <T> Future<T> submit(Runnable task, T result);
    //返回一个Future对象,经过返回的Future对象,咱们能够检查提交的任务是否执行完成了。 
    Future<?> submit(Runnable task);
    
    //返回一个Future的List,其中对应着每一个Callable任务执行后的Future对象。
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException;
    //增长了超时控制 
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                  long timeout, TimeUnit unit)
        throws InterruptedException;
        
        
    //接收参数是一个Callable的集合,
    //返回的是全部Callable集合任务中某一个任务的执行结果
    <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;
    //增长了超时控制
    <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
复制代码

ExecutorService 再Executor接口的基础上扩展了对线程池状态的控制以及提交任务执行的超时控制。线程池的基本功能还不够完善,不能真正的具有处理具体业务的能力(毕竟是个接口,O(∩_∩)O哈哈~)。

开个篇,慢慢学~

相关文章
相关标签/搜索