线程池

    技术2022-05-19  23

    http://www.chinaitpower.com/A200507/2005-07-24/166973.html  digester解析xml

     

    jdk1.5自带线程池的学习

    ExecutorService ex=Executors.newCachedThreadPool();  根据需要无限大容量的线程池

    ExecutorService ex=Executors.newFixedThreadPool(int count)  创建固定大小的线程池

    ScheduledExecutorService sc=Executors.newScheduledThreadPool(int corePoolSize)  可延迟重复执行的线程池(Timer)

    sc.scheduleAtFixedRate(Runnable command, long initialDelay,long period,TimeUnit unit)

           initialDelay:初始执行时间:

           period:间隔执行时间

           unit:间隔时间单位

     

     

    类ThreadPoolExecutor提供更精确控制的线程池参数,功能更强大。一般构造方法是

    public ThreadPoolExecutor(int corePoolSize,                          int maximumPoolSize,                          long keepAliveTime,                          TimeUnit unit,                          BlockingQueue<Runnable> workQueue,   (示例:new ArrayBlockingQueue<Runnable>(3))                          RejectedExecutionHandler handler  (示例:new ThreadPoolExecutor.CallerRunsPolicy)

                              )

    用给定的初始参数创建新的 ThreadPoolExecutor。

     

    参数: corePoolSize - 池中所保存的线程数,包括空闲线程。 maximumPoolSize - 池中允许的最大线程数。 keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。 unit - keepAliveTime 参数的时间单位。 workQueue - 执行前用于保持任务的队列。此队列仅由保持 execute 方法提交的 Runnable 任务。 handler - 由于超出线程范围和队列容量而使执行被阻塞时所使用的处理程序

    执行顺序:先创建到初始线程量,不够了放到队列里,再不够启动到最大线程,执行,最后执行队列里的。


    最新回复(0)