美文网首页Android开发
线程池示例(二)Executors.newScheduledTh

线程池示例(二)Executors.newScheduledTh

作者: 乌鲁木齐001号程序员 | 来源:发表于2019-03-31 17:49 被阅读0次

示例1 - 延迟执行

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(int corePoolSize);

        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                log.warn("schedule run");
            }
        }, 3, TimeUnit.SECONDS);
    }

}

输出:

17:40:36.288 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run

示例2 - 周期性执行

  • 延迟1秒后,每隔3秒执行一次;
  • 不适合理解shutdown(),需要等待一定的契机;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                log.warn("schedule run");
            }
        }, 1, 3, TimeUnit.SECONDS);
    }

}

输出:

17:43:28.481 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:31.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:34.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run
17:43:37.478 [pool-1-thread-1] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - schedule run

示例3 - 用Timer周期性执行

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.warn("timer run");
            }
        }, new Date(), 5 * 1000);
    }
}

输出:

17:48:13.543 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run
17:48:18.540 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run
17:48:23.541 [Timer-0] WARN com.example.concurrency.example.threadPool.ThreadPoolExample4 - timer run

相关文章

  • 线程池示例(二)Executors.newScheduledTh

    示例1 - 延迟执行 输出: 17:40:36.288 [pool-1-thread-1] WARN com.ex...

  • Springboot | 线程池的学习,多线程池配置示例

    一、线程和进程,线程的生命周期二、单线程和多线程三、线程池的概念四、线程池的使用五、多线程池配置示例 一、线程和进...

  • Thread

    队列 线程锁 多线程,线程池 队列 多线程爬虫示例 多线程 自定义线程 线程池

  • Python多线程

    目录:一、线程的创建二、多线程互斥锁三、线程间通信四、线程池 Python并发之多线程 一、线程的创建 单线程示例...

  • java 实现自定义线程池

    java 实现自定义线程池 定义线程池接口 线程池接口的默认实现 示例摘抄于《Java并发变成的艺术》4.4.3线...

  • juc9-Executors

    0 线程池类图 一 ForkJoinPool 分治线程池 二 ThreadPoolExecutor 固定线程数线程...

  • Java多线程4 初识线程池

    前言 Java为什么引入线程池?创建线程示例 new Thread的弊端 每次new Thread新建对象性能差。...

  • Java常见线程池示例

      Java通过Executors提供了四种常见的线程池框架,分别为:newFixedThreadPool:创建一...

  • 线程池技术及其示例

    服务端没接受一次任务就创建一个线程,如果成千上万次请求,那么将创建万计的线程,这样会导入服务器频繁的进行线程的切换...

  • 线程池--完整使用示例

    执行结果: 从执行结果可以看出,当线程池中线程的数目大于5时,便将任务放入任务缓存队列里面,当任务缓存队列满了之后...

网友评论

    本文标题:线程池示例(二)Executors.newScheduledTh

    本文链接:https://www.haomeiwen.com/subject/pkeubqtx.html