1.Executor

接口源码:

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);
}

接口使用:

public class T01_MyExecutor implements Executor {

    public static void main(String[] args) {
new T01_MyExecutor().execute(()->System.out.println("hello executor"));
} @Override
public void execute(Runnable command) {
//new Thread(command).run();
command.run();
} }

2.ExecutorService

源码:

认识submit方法,扩展了execute方法,具有一个返回值

<T> Future<T> submit(Callable<T> task);

<T> Future<T> submit(Runnable task, T result);

Future<?> submit(Runnable task);

使用:

ExecutorService提供了管理Eecutor生命周期的方法,ExecutorService的生命周期包括了:运行  关闭和终止三种状态。
 
ExecutorService在初始化创建时处于运行状态。
shutdown方法等待提交的任务执行完成并不再接受新任务,在完成全部提交的任务后关闭
shutdownNow方法将强制终止所有运行中的任务并不再允许提交新任务
 
可以将一个Runnable或Callable提交给ExecutorService的submit方法执行,最终返回一上Futire用来获得任务的执行结果或取消任务
(任务执行完成后并返回执行结果)

public class CallableAndFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> future = executor.submit(new Callable<String>() {   //接受一上callable实例
            public String call() throws Exception {
                return "MOBIN";
            }
        });
        System.out.println("任务的执行结果:"+future.get());
    }
}
输出:
任务的执行结果:MOBIN

3.Executors

提供了一系列静态工厂方法用于创建各种线程池

   newFixedThreadPool:创建可重用且固定线程数的线程池,如果线程池中的所有线程都处于活动状态,此时再提交任务就在队列中等待,直到有可用线程;如果线程池中的某个线程由于异常而结束时,线程池就会再补充一条新线程。
ScheduledExecutorService executor = Executors.newScheduledThreadPool();

4.ThreadPool

线程池的概念

public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(); //execute submit
for (int i = ; i < ; i++) {
service.execute(() -> {
try {
TimeUnit.MILLISECONDS.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
}
System.out.println(service); service.shutdown();
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
System.out.println(service); TimeUnit.SECONDS.sleep();
System.out.println(service.isTerminated());
System.out.println(service.isShutdown());
System.out.println(service);
}

5.Future

public static void main(String[] args) throws InterruptedException, ExecutionException {

        FutureTask<Integer> task = new FutureTask<>(()->{
TimeUnit.MILLISECONDS.sleep();
return ;
}); //new Callable () { Integer call();} new Thread(task).start(); System.out.println(task.get()); //阻塞 //*******************************
ExecutorService service = Executors.newFixedThreadPool();
Future<Integer> f = service.submit(()->{
TimeUnit.MILLISECONDS.sleep();
return ;
});
System.out.println(f.get());
System.out.println(f.isDone()); }

6.

Java线程池相关类-Executor框架的更多相关文章

  1. java线程池相关接口Executor和ExecutorService

    在线程池的api中,Executor接口是最上层的接口,内部只有一个方法.如下: public interface Executor { void execute(Runnable command); ...

  2. java线程池相关知识点总结

    Android中常见到的很多通用组件一般都离不开"池"的概念,如各种图片加载库,网络请求库,即使Android的消息传递机制中的Meaasge当使用Meaasge.obtain() ...

  3. Java线程池ThreadPoolExecutor类源码分析

    前面我们在java线程池ThreadPoolExecutor类使用详解中对ThreadPoolExector线程池类的使用进行了详细阐述,这篇文章我们对其具体的源码进行一下分析和总结: 首先我们看下T ...

  4. Java线程池 ThreadPoolExecutor类

    什么是线程池? java线程池是将大量的线程集中管理的类, 包括对线程的创建, 资源的管理, 线程生命周期的管理. 当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程, 从而减少系 ...

  5. java线程池ThreadPoolExecutor类使用详解

    在<阿里巴巴java开发手册>中指出了线程资源必须通过线程池提供,不允许在应用中自行显示的创建线程,这样一方面是线程的创建更加规范,可以合理控制开辟线程的数量:另一方面线程的细节管理交给线 ...

  6. 含源码解析,深入Java 线程池原理

    从池化技术到底层实现,一篇文章带你贯通线程池技术. 1.池化技术简介 在系统开发过程中,我们经常会用到池化技术来减少系统消耗,提升系统性能. 在编程领域,比较典型的池化技术有: 线程池.连接池.内存池 ...

  7. (Java 多线程系列)Java 线程池(Executor)

    线程池简介 线程池是指管理同一组同构工作线程的资源池,线程池是与工作队列(Work Queue)密切相关的,其中在工作队列中保存了所有等待执行的任务.工作线程(Worker Thread)的任务很简单 ...

  8. 从使用到原理学习Java线程池

    线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收. 所 ...

  9. 【转载】从使用到原理学习Java线程池

    线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收. 所 ...

随机推荐

  1. pymysql模块使用

    一.写函数的原因 写这个函数的原因就是为了能够不每次在用Python用数据库的时候还要在写一遍  做个通用函数做保留,也给大家做个小小的分享,函数不是最好的,希望有更好的代码的朋友能提出 互相学习 二 ...

  2. h5移动端聊天室|仿微信界面聊天室|h5多人聊天室

    今年的FIFA世界杯甚是精彩,最近兴致高涨就利用HTML5开发了一个手机端仿微信界面聊天室,该h5聊天室采用750px全新伸缩flex布局,以及使用rem响应式配合fontsize.js,页面弹窗则是 ...

  3. error 'there is already an open datareader associated with this command which must be closed first'

    This can be easily solved by allowing MARS in your connection string. Add MultipleActiveResultSets=t ...

  4. [WiX]Component Rules 101

    原文:http://robmensching.com/blog/posts/2003/10/18/component-rules-101 I've been debating with myself ...

  5. SAS->关联分析实践

    SAS系统被誉为国际上的标准软件系统,本文将详细介绍如何在SAS/EM模块中进行关联规则数据挖掘,使用的软件版本是SAS 9.1.3下的Enterprise Miner 4.3: 从SAS顶端的[解决 ...

  6. Shell之expect的测试

    测试:./sshLogin.sh Slave1 caipeichao 1qaz@WSX hadoop lk198981 HadoopCluster #!/usr/bin/expect -f #auto ...

  7. 使用Hive UDF和GeoIP库为Hive加入IP识别功能

    Hive是基于Hadoop的数据管理系统,作为分析人员的即时分析工具和ETL等工作的执行引擎,对于如今的大数据管理与分析.处理有着非常大的 意义.GeoIP是一套IP映射数据库,它定时更新,并且提供了 ...

  8. 机器学习实战(一)k-近邻算法

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7593656.html 1.原理 本章介绍机器学习实战的第一个算法——k近邻算法(k Nearest Neighb ...

  9. 机器学习-KNN算法

    原理 KNN算法,又叫K近邻算法.就是在训练集中数据和标签已知的情况下,输入测试数据,将测试数据的特征与训练集中对应的特征进行相互比较,找到训练集中与之最为相似的前K个数据,则该测试数据对应的类别就是 ...

  10. vue 监听对象里的特定数据

    vue  监听对象里的特定数据变化 通常是这样写的,只能监听某一个特定数据 watch: { params: function(val) { console.log(val) this.$ajax.g ...