Java线程池的使用

/ Java技术 / 没有评论 / 2044浏览

alt

使用线程池的原则

public ThreadPoolExecutor(int corePoolSize,
                            int maximumPoolSize,
                            long keepAliveTime,
                            TimeUnit unit,
                            BlockingQueue<Runnable> workQueue)
  1. corePoolSize:核心线程数,默认情况下核心线程会一直存活,即使处于闲置状态也不会受存keepAliveTime限制。除非将allowCoreThreadTimeOut设置为true
  2. maximumPoolSize:线程池所能容纳的最大线程数。超过这个数的线程将被阻塞。当任务队列为没有设置大小的LinkedBlockingDeque时,这个值无效。
  3. keepAliveTime:非核心线程的闲置超时时间,超过这个时间就会被回收。
  4. unit:指定keepAliveTime的单位,如TimeUnit.SECONDS。当将allowCoreThreadTimeOut设置为true时对corePoolSize生效。
  5. workQueue:线程池中的任务队列.常用的有三种队列,SynchronousQueue,LinkedBlockingDeque,ArrayBlockingQueue

可执行定时任务的线程池

包含两种使用方法scheduleWithFixedDelayscheduleAtFixedRate

scheduleWithFixedDelay本次任务执行结束后等待指定延时再执行下次任务,下一次任务受本次计划执行时间的影响。延时计时从本次任务结束开始。

    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);
  1. command:he task to execute
  2. initialDelay:the time to delay first execution
  3. delay:本次任务结束后延时时间
  4. unit:延时时间单位
t1 start    t1 end/delay start    delay end/t2 start 
↓            ↓                     ↓
|____________|_____________________|__________
    try{
            ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
            scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    try{
                        System.out.println(Thread.currentThread().getName()+":task1:"+ DateTime.now().toString("yyyy-MM-dd hh🇲🇲ss"));
                        Thread.sleep(3000);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }

                }
            }, 0, 3, TimeUnit.SECONDS);
            System.in.read();
        } catch (IOException e){
            e.printStackTrace();
    }

scheduleAtFixedRate延时计时从本次任务开始就计时,如果delay大于任务执行时间则在delay时间后执行下次任务,如果delay小于任务执行时间。则在本次任务结束后开始下次人去

    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);
  1. command:he task to execute
  2. initialDelay:the time to delay first execution
  3. delay:从本次任务开始的延时时间
  4. unit:延时时间单位
t1 start/delay start   delay end    t1 end/t2 start/delay start
↓                          ↓                ↓
|__________________________|________________|______________________
        try{
            ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
            scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    try{
                        System.out.println(Thread.currentThread().getName()+":task2:"+ DateTime.now().toString("yyyy-MM-dd hh🇲🇲ss"));
                        Thread.sleep(3000);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }

                }
            }, 0, 1, TimeUnit.SECONDS);
            System.in.read();
        } catch (IOException e){
            e.printStackTrace();
        }