缓存线程池-newCachedThreadPool (搬迁至此)
2012-10-06 18:42
399 查看
缓存线程池与固定线程池的区别在于对于需要执行很多短期异步任务的程序来说,缓存线程池可以提高程序性能,因为长时间保持空闲的这种类型的线程池不会占用任何资源,调用缓存线程池对象将重用以前构造的线程(线程可用状态),若线程没有可用的,则创建一个新线程添加到池中,缓存线程池将终止并从池中移除60秒未被使用的线程。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCahedThread {
public static void main(String[] args){
ExecutorService exec=Executors.newCachedThreadPool();
for(int index=0;index<10;index++){
Runnable run=new Runnable(){
public void run(){
long time=(long)(Math.random()*1000);
System.out.println("sleep:"+time+" ss ");
try{
Thread.sleep(time);
}catch (Exception e) {
// TODO: handle exception
}
}
};
exec.execute(run);
}
exec.shutdown();
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCahedThread {
public static void main(String[] args){
ExecutorService exec=Executors.newCachedThreadPool();
for(int index=0;index<10;index++){
Runnable run=new Runnable(){
public void run(){
long time=(long)(Math.random()*1000);
System.out.println("sleep:"+time+" ss ");
try{
Thread.sleep(time);
}catch (Exception e) {
// TODO: handle exception
}
}
};
exec.execute(run);
}
exec.shutdown();
}
}
相关文章推荐
- 线程池之newCachedThreadPool可缓存线程池
- 8、java5线程池之动态缓存线程池newCachedThreadPool
- 固定线程池-newFixedThreadPool (搬迁至此)
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 线程池的使用(newCachedThreadPool)
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 四种线程池创建及使用(一)newScheduledThreadPool和newCachedThreadPool的使用方法详解
- java程序设计线程池(newCachedThreadPool())
- 线程池-newFixedThreadPool、newCachedThreadPool、
- Executors 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- newfixedthreadpool线程池 与newCachedThreadPool 的区别
- 浅谈Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Android创建全局线程池类之newCachedThreadPool
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor