缓存线程池-newCachedThreadPool (搬迁至此)
来源:互联网 发布:淘宝3c证书要怎么填 编辑:程序博客网 时间:2024/05/06 04:57
缓存线程池与固定线程池的区别在于对于需要执行很多短期异步任务的程序来说,缓存线程池可以提高程序性能,因为长时间保持空闲的这种类型的线程池不会占用任何资源,调用缓存线程池对象将重用以前构造的线程(线程可用状态),若线程没有可用的,则创建一个新线程添加到池中,缓存线程池将终止并从池中移除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 (搬迁至此)
- 固定线程池-newFixedThreadPool (搬迁至此)
- 任务计划线程池-ScheduledExecutorService(搬迁至此)
- 线程同步辅助-CyclicBarrier(搬迁至此)
- 线程同步辅助-CountDownLatch(搬迁至此)
- 返回结果线程—等待单个线程(搬迁至此)
- 返回结果的线程-等待一组线程(搬迁至此)
- 线程池ExecutorService newCachedThreadPool()
- java线程池newCachedThreadPool
- Java newCachedThreadPool 创建线程池
- 技术方面计划搬迁至此...
- newfixedthreadpool线程池 与newCachedThreadPool 的区别
- Java线程池newSingleThreadExecutor newFixedThreadPool newCachedThreadPool newScheduledThreadPool
- java线程池(四) Executors.newCachedThreadPool
- 阻塞队列-BlockingQueue (搬迁至此)
- 新浪微博搬迁至此声明
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java线程池(newCachedThreadPool、newFixedThreadPool、newScheduledThreadPool 、newSingleThreadExector )
- ASP.NET未处理异常(Unhandled Exception)调试试验
- hdu 4294 Multiple
- C++内存管理之一(检测内存泄露)
- VC内存泄漏检测工具_Visual Leak Detector
- c++返回对象值时的copy、=、析构函数的调用顺序
- 缓存线程池-newCachedThreadPool (搬迁至此)
- 【转】Objective-C Message
- 线程同步辅助-CyclicBarrier(搬迁至此)
- Little Test
- hdu 4293 Groups
- 线程同步辅助-CountDownLatch(搬迁至此)
- 阻塞队列-BlockingQueue (搬迁至此)
- 返回结果线程—等待单个线程(搬迁至此)
- 返回结果的线程-等待一组线程(搬迁至此)