应用Executors来建立Thread pool
来源:互联网 发布:win10磁盘优化需要几遍 编辑:程序博客网 时间:2024/05/16 19:21
Java Gossip: Executors
有时候您需要建立一堆Thread来执行一些小任务,然而频繁的建立Thread有时会是个开销,因为Thread的建立必须与作业系统互动,如果能建立一个Thread pool来管理这些小的Thread并加以重复使用,对于系统效能会是个改善的方式。您可以使用Executors来建立Thread pool,Executors有几个static方法,列出如下:
方法 说明 newCachedThreadPool 建立可以快取的Thread,每个Thread预设可idle 60秒
newFixedThreadPool
包括固定数量的ThreadnewSingleThreadExecutor
只有一个Thread,循序的执行指定给它的每个任务 newScheduledThreadPool 可排程的Thread newSingleThreadScheduledExecutor 单一可排程的Thread举个简单的实例,下面的程式使用newFixedThreadPool方法建立Thread pool,当中包括五个可以重复使用的Thread,您可以指定Runnable物件给它,程式中会产生十个Runnable物件,由于Thread pool中只有五个可用的Thread,所以后来建立的五个Runnable必须等待有空闲的Thread才会被执行:
- ExecutorDemo.java
package onlyfun.caterpillar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorDemo {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(5);
for(int i = 0; i < 10; i++) {
final int count = i;
service.submit(new Runnable() {
public void run() {
System.out.println(count);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
service.shutdown(); // 最后记得关闭Thread pool
}
}
submit()方法也接受实作Callable介面的物件,最后传回Future物件,可以取得Callable执行过后的传回结果。
如果想利用Executors进行排程,例如排定某个工作30秒后执行:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor( );
scheduler.schedule(new Runnable( ) {
public void run() {
// 排程工作
}
},
30, TimeUnit.SECONDS);
或排定某个工作5秒后执行,之后每30秒执行一次:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor( );
final ScheduledFuture future = scheduler.scheduleAtFixedRate(new Runnable( ) {
public void run() {
// 排程工作
System.out.println("t");
}
},
0, 5, TimeUnit.SECONDS);
// 排定 60 秒后取消future
scheduler.schedule(new Runnable( ) {
public void run( ) {
future.cancel(false);
}
}, 60, TimeUnit.SECONDS);
如上所示,想要取消排程任务,可以呼叫ScheduledFuture的cancel()方法。
<script type="text/javascript"><!--google_ad_client = "pub-9750319131714390";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1922563
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 应用Executors来建立Thread pool
- 【Java多线程】之十二:Java Thread Pool Example using Executors and ThreadPoolExecutor
- Spring Thread Pool 线程池的应用
- 好书共享: 《感受微软:比尔.盖茨的12个小秘密》
- ERP流程重组常见错误攻略
- 好书共享:《微软项目:求生法则》
- 各取所需 看看ERP为不同员工带来了什么
- 信息化故事--温州的传奇(11)从“进城务工”看“温州新版自闭症”
- 应用Executors来建立Thread pool
- wxPython application behaves different after being packed by py2exe.
- 项目管理手记(七)--DRP系统的文化输出与营销
- 学习ZARA模式要从老板观念转变开始
- 甲方屡次选错乙方终致项目暴毙
- [转帖]项目管理驾御术
- 出差见闻(一)--温州的出租车:起步价15元
- Keytool命令来生成CA数字证书
- 好书共享: 《感受微软:比尔.盖茨的12个小秘密》