`

Executor usage

阅读更多

Executor is used to arrange thread execution. Basicly speaking, just manage, how many threads are permited to run together.

 

ExecutorService obj = Executors.newSingleThreadExecutor( ) -- only 1

ExecutorService obj = Executors.newFixedThreadPool(int poolSize) -- fix amount

ExecutorService obj = Executors.newCachedThreadPool( ) --- as many as it can

 

Code Sample

 

Executor e = Executors.newFixedThreadPool(5);

e.execute(new RunnableTask1( ));

e.execute(new RunnableTask2( )); 

e.execute(new RunnableTask3( ));

 

 

While in another aritcle, you can see that, Executors.newFixedThreadPool(5) returns a ExecutorService. Actually, ExecutorService is a subclass of Executor, and it supplies more functionalities than Executor.

 

 

 

 As far as ExecutorService is concerned, it supplies only one advanced function than Executor---how to stop the threads.

 

service.shutdownNow(); //Try to finish all Runnables(started and not started(in queue)), then, stop Executor.

service.shutdown(); //Directly stop the Executor, terminate running Runables, return back not started, yet in queue Runables.

 

There is a internal Queue maintained by ExecutorService. That's the core idea.

 

 

 ------------------------------------------------------------------------------------

Another point, Please pay attention to how they are used to different targets.

 

//FutureObject = ExecutorService.submit(Callable object);

//Executor.execut(Runnable object); //without any returns.

Future<BigInteger> prime1 = service.submit(new RandomPrimeSearch(512));

Future<BigInteger> prime2 = service.submit(new RandomPrimeSearch(512));

Future<BigInteger> prime3 = service.submit(new RandomPrimeSearch(512));

 

 

 

 

 

 

 

 


  


  
分享到:
评论

相关推荐

    prometheus-am-executor:根据Prometheus警报执行命令

    普罗米修斯执行者 ...Usage: ./prometheus-am-executor [options] script [args..] -f string YAML config file to use -l string HTTP Port to listen on (default ":8080") -v Enable verbose/debug log

    nagios_client

    Nagios分为服务器端(Nagios Core)和客户端(NRPE,Nagios Remote Plugin Executor)。服务器端负责监测和报警,客户端则执行实际的监控检查。在本案例中,我们将关注客户端的部署。 二、客户端安装 1. **nagios...

    ambari-azkaban-service:阿兹卡班(Azkaban)的Ambari服务

    Intro Ambari 集成 Azkaban 使用前,先clone.代码到本地,选择合适你的分支版本。 Major Project Structure configuration : azkaban 配置文件 bin : Azkaban脚本修改(单机部署web、exec需要更换) ...Usage

    Activiti_V5.21_用户手册_中文版_完整目录书签.pdf

    3.4.1. 使用 Usage 3.4.2. 配置 Configuration 3.5. 支持的数据库 Supported databases 3.6. 创建数据库表 Creating the database tables 3.7. 数据库表名说明 Database table names explained...

    selenium webdriverAPI-python

    The `execute` method sends a command to be executed by a command executor. - `driver_command`: The name of the command to execute as a string. - `params`: A dictionary of named parameters to send ...

    tomcat cpu high

    检查`server.xml`中的`&lt;Executor&gt;`或`&lt;Connector&gt;`配置,确保线程池大小适中,避免过度创建线程。 2. **内存泄漏**:Java应用程序中的内存泄漏可能导致GC(垃圾收集)频繁运行,从而占用大量CPU。使用内存分析工具...

    TCP-IP Sockets in Java. Practical Guide for Programmers

    New abstractions to be covered include NetworkInterface, InterfaceAddress, Inet4/6Address, SocketAddress/InetSocketAddress, Executor, and others; extended access to low-level network information; ...

    藏经阁-Sparklint a Tool for Identifyi.pdf

    动态资源分配(Dynamic Allocation)在某些情况下可能并不理想,如长时间的executor空闲超时设置,可能导致在应用开始时效果明显,但随着作业运行,资源回收的开销反而降低了效率,表现为频繁的executor重启...

    dl-infer-perf:深度学习推理性能分析

    dl-推断性能 在pytorch / tensorflow和TensorRT / XLA / TVM上进行深度学习推理性能的性能分析。 环境环境 TVM 码头工人:nvidia / cuda:11.1.1-devel-...usage: executor.py [-h] [-w WARMUP] [-r ROUNDS] [-s SIZE]

    快速找出php中可能导致cpu飙升问题的代码行__1.docx

    7. **内存分析**: 使用`memory_get_usage()`和`memory_get_peak_usage()`检查内存使用情况,内存泄漏也可能导致CPU飙升。 8. **错误报告**: 检查错误日志,错误处理不当可能会导致无限循环或资源浪费。 9. **分析...

    REXX GCI-开源

    REXX(Regulated eXtended eXecutor)是一种高级过程编程语言,以其简洁的语法和强大的文本处理能力而闻名。在“REXX GCI-开源”项目中,GCI(Generic Call Interface)是REXX的一个关键组件,它为程序员提供了一种...

    python3.6.5参考手册 chm

    Performance and resource usage PEP 397: Python Launcher for Windows PEP 3151: Reworking the OS and IO exception hierarchy PEP 380: Syntax for Delegating to a Subgenerator PEP 409: Suppressing ...

Global site tag (gtag.js) - Google Analytics