定时线程池执行任务时任务执行时间与定时关系

当执行时间小于定时时间的时候:

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ScheduledThreadPoolExample {

    public static void main(String[] args) {
        System.out.println("执行的时间小于设定的周期");
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("start task");
                    Thread.sleep(5000);
                    log.info("done");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 3000, 8000, TimeUnit.MILLISECONDS);
    }
}

执行结果:

执行的时间小于设定的周期
15:39:11.944 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
15:39:16.960 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - done
15:39:19.938 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
15:39:24.951 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - done
15:39:27.936 [pool-1-thread-1] INFO com.hovel.base.thread.pool.SchExample - start task
.......


当执行任务时间大于定时时间

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ScheduledThreadPoolExample {

    public static void main(String[] args) {
        System.out.println("执行的时间小于设定的周期");
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("start task");
                    Thread.sleep(8000);
                    log.info("done");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 3000, 5000, TimeUnit.MILLISECONDS);
    }
}

结果:

执行的时间大于设定的周期
15:44:31.404 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
15:44:39.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
15:44:39.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
15:44:47.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
15:44:47.414 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
15:44:55.415 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - done
15:44:55.415 [pool-1-thread-1] INFO com.hovel.base.thread.pool.ScheduledThreadPoolExample - start task
.......

由上面的两组执行结果对比,可知,定时任务都是串行执行的。当任务执行的时间小于定时时间的时候,定时的时间减去任务执行的时间即是下一次任务执行的时间;但是当任务执行时间大于定时时间的时候就有所不同了,即是定时时长小于任务时长,但是依然会等到任务结束后再执行下一次定时任务,而不是在任务未执行完时就开始下一次任务,只不过这个时候,下一次执行紧挨执行。

你可能感兴趣的:(JAVA多线程,Java,多线程,线程池)