应用Executors来建立Thread pool
来源:互联网 发布:如何评价鬼吹灯 知乎 编辑:程序博客网 时间:2024/06/05 21:05
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> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1197007384156&lmt=1181153050&format=160x600_as&output=html&correlator=1197007384109&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossipCN%2FJavaGossip-V2%2FExcutors.htm&ad_type=text_image&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dgb2312%26bs%3DExecutorService%26sr%3D%26z%3D%26cl%3D3%26f%3D8%26wd%3DExecutors%26ct%3D0&cc=384&ga_vid=1519114475.1197007384&ga_sid=1197007384&ga_hid=1360029615&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
<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> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1197007384187&lmt=1181153050&prev_fmts=160x600_as&format=160x600_as&output=html&correlator=1197007384187&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossipCN%2FJavaGossip-V2%2FExcutors.htm&ad_type=text_image&ref=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dgb2312%26bs%3DExecutorService%26sr%3D%26z%3D%26cl%3D3%26f%3D8%26wd%3DExecutors%26ct%3D0&cc=384&ga_vid=1519114475.1197007384&ga_sid=1197007384&ga_hid=1360029615&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
- 应用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 线程池的应用
- 理解Python命名机制
- Vmware和Xen在Linux虚拟化补丁上冲突
- 为什么大公司的产品越做越烂
- 创新者启动collabnet.org电邮
- CSDN Windows专区网络版版主测试题目答案
- 应用Executors来建立Thread pool
- SYBASE编程中的莫名错误及其解决办法
- C++ Under The Hood(中译版)
- 《给初学者的Windows Vista的补遗手册》之041
- 开始全心投入《深入Spring 2:轻量级J2EE开发框架原理与实践》
- 让绵羊变成狮子---创造学习型营销团队[转]
- Windows 98 第二版的关机问题
- [转]2005年乔布斯在斯坦福大学的演讲
- Richard Stallman采访录音@巴塞罗纳GPLv3会议20060623