zoukankan      html  css  js  c++  java
  • 缓存线程池的作用

    JAVA提供4种缓存线程池

    • newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
    • newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
    • newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
    • newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    /**
     * 缓存线程池
     * 缓存线程池可以提高程序性能
     * 长时间闲置的不会占用资源
     * */

    public class MyCache {

        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) {
                        }
                    }
                };
                exec.execute(run);
            }
            exec.shutdown();
        }

    }

    当一个人在成长过程中,慢慢的享受学习,那么这个人就在成长,在往自己目标的方向奔跑.
  • 相关阅读:
    MySQL函数大全
    Hibernate的理论知识点
    捕获异常
    重定向到其他的页面
    Jquery中val、text、html的区别
    条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
    inline-block元素的4px空白间距解决方案
    img标签中alt属性与title属性
    3像素文本偏移bug 解决方案
    google Ip
  • 原文地址:https://www.cnblogs.com/zique/p/6152845.html
Copyright © 2011-2022 走看看