栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java线程与线程池

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java线程与线程池

Java线程与线程池
    • java线程创建方式
    • 线程池体系
    • Executor
    • ExecutorService
    • AbstractExecutorService

java线程创建方式
  • 继承thread
  • 实现runable接口
  • 实现callable和future
线程池体系

Executor

抽象出定义一个执行无返回值的任务方法

public interface Executor {
    void execute(Runnable command);
}
ExecutorService

抽象出定义了线程池的动作

public interface ExecutorService extends Executor {
    //关闭
    void shutdown();

    List shutdownNow();
    
    boolean isShutdown();
   
    boolean isTerminated();

    boolean awaitTermination(long timeout, TimeUnit unit)  throws InterruptedException;
    //提交任务
     Future submit(Callable task);

     Future submit(Runnable task, T result);

    Future submit(Runnable task);
     //执行所有任务
     List> invokeAll(Collection> tasks) throws InterruptedException;

     List> invokeAll(Collection> tasks, long timeout, TimeUnit unit)  throws InterruptedException;
    //执行任一任务
     T invokeAny(Collection> tasks)  throws InterruptedException, ExecutionException;

     T invokeAny(Collection> tasks, long timeout, TimeUnit unit)throws InterruptedException, ExecutionException, TimeoutException;
}
AbstractExecutorService

使用模板方法抽象定义了一些线程池常用的方法,都会调用子类实现的execute(ftask)方法。

   public  Future submit(Callable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture ftask = newTaskFor(task);
        execute(ftask);
        return ftask;
    }
##  RunnableFuture ftask = newTaskFor(task);
 protected  RunnableFuture newTaskFor(Callable callable) {
        return new FutureTask(callable);
 }

FutureTask继承关系:

Future接口定义了一些获取结果的包装方法。

public interface Future {

	 boolean cancel(boolean mayInterruptIfRunning);
	 boolean isCancelled();
	 boolean isDone();
	 V get() throws InterruptedException, ExecutionException;
	 V get(long timeout, TimeUnit unit)
	        throws InterruptedException, ExecutionException, TimeoutException;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号