一  定时任务

package com.aaa.threaddemo;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.xml.crypto.Data; /*
* 一 newScheduledThreadPool 是个啥?
* 1 是一个线程池
* 2 可定时执行任务
* 3 核心线程数是固定的
* 4 非核心线程数 2147483647
*
*
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
} public ScheduledThreadPoolExecutor(int corePoolSize) {
super(
corePoolSize,
Integer.MAX_VALUE,
0, //非核心线程的生存时间是 0 马上回收。
NANOSECONDS,
new DelayedWorkQueue()); //一个优先级的队列 阻塞的队列,因为它实现了BlockingQueue } DelayedWorkQueue() 优先级队列怎么用的?
队列一般都是先进先出的,但是在定时/延时任务中,我们需要的是延迟时间短的任务先执行。
为了解决这个问题,需要用到一种特殊的队列
【优先级队列】
对插入的数据进行优先级的排序,保证优先级高的数据先执行。和往队列中插入的顺序无关。
没能深入调查。。。。 二 提供了两个方法可以用 schedule(command, delay, unit) schedule(
task, 需要执行的任务
3, 间隔的时间
TimeUnit.SECONDS 时间单位 秒 分 时 。。。
); scheduleAtFixedRate(
command,
initialDelay, 初次执行 间隔的时间
period, 再次执行的相隔时间
unit 时间的单位
)
【注意!】
如果只有一次间隔时间,线程结束后关闭线程池即可。 schedule(command, delay, unit)
但是当有二次间隔时间,是不能将线程池 shutdown的! scheduleAtFixedRate(command, initialDelay, period, unit) *
*/
public class ScheduledThreadPoolDemo {
public static void main(String[] args) {
Integer integer = new Integer(0);
int maxValue = integer.MAX_VALUE;
System.out.println("[Integer.MAX_VALUE] = " + maxValue); SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //验证是否是在三秒后执行
System.out.println(df2.format(System.currentTimeMillis())); ExecutorService scheduledPool = Executors.newScheduledThreadPool(1); //创建定时的线程池 核心数量 2
ScheduledThreadDemo thread = new ScheduledThreadDemo(); Runnable task2 = new Runnable() {
public void run() {
System.out.println("开启任务 task2");
}
}; ((ScheduledExecutorService) scheduledPool).schedule(thread,3,TimeUnit.SECONDS); //1.对于thread 2. 延迟时间3后执行任务 3.单位TimeUnit.SECONDS 是 【秒】 三秒后执行
((ScheduledExecutorService) scheduledPool).schedule(task2,5,TimeUnit.SECONDS); //延迟5秒后执行任务 scheduledPool.shutdown();
}
} class ScheduledThreadDemo extends Thread{
@Override
public void run() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(System.currentTimeMillis()));
System.out.println("我是线程1, 我在运行");
System.out.println("正在运行的线程名字" + Thread.currentThread().getName());
} }

看结果

二  scheduleAtFixedRate ?

package com.aaa.threaddemo;

import static java.util.concurrent.TimeUnit.NANOSECONDS;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.xml.crypto.Data; /*
* 一 newScheduledThreadPool 是个啥?
* 1 是一个线程池
* 2 可定时执行任务
* 3 核心线程数是固定的
* 4 非核心线程数 2147483647
*
*
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
} public ScheduledThreadPoolExecutor(int corePoolSize) {
super(
corePoolSize,
Integer.MAX_VALUE,
0, //非核心线程的生存时间是 0 马上回收。
NANOSECONDS,
new DelayedWorkQueue()); //一个优先级的队列 阻塞的队列,因为它实现了BlockingQueue } DelayedWorkQueue() 优先级队列怎么用的?
队列一般都是先进先出的,但是在定时/延时任务中,我们需要的是延迟时间短的任务先执行。
为了解决这个问题,需要用到一种特殊的队列
【优先级队列】
对插入的数据进行优先级的排序,保证优先级高的数据先执行。和往队列中插入的顺序无关。
没能深入调查。。。。 二 提供了两个方法可以用 schedule(command, delay, unit) schedule(
task, 需要执行的任务
3, 间隔的时间
TimeUnit.SECONDS 时间单位 秒 分 时 。。。
); scheduleAtFixedRate(
command,
initialDelay, 初次执行 间隔的时间
period, 再次执行的相隔时间
unit 时间的单位
)
【注意!】
如果只有一次间隔时间,线程结束后关闭线程池即可。 schedule(command, delay, unit)
但是当有二次间隔时间,是不能将线程池 shutdown的! scheduleAtFixedRate(command, initialDelay, period, unit) *
*/
public class ScheduledThreadPoolDemo {
public static void main(String[] args) {
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //验证是否是在三秒后执行
System.out.println(df2.format(System.currentTimeMillis())); ExecutorService scheduledPool = Executors.newScheduledThreadPool(1); //创建定时的线程池 核心数量 2
ScheduledThreadDemo thread = new ScheduledThreadDemo(); Runnable task2 = new Runnable() {
public void run() {
System.out.println("开启任务 task2");
}
}; Runnable task3 = new Runnable() {
public void run() {
System.out.println("开启任务 task3");
}
}; ((ScheduledExecutorService) scheduledPool).schedule(thread,3,TimeUnit.SECONDS); //1.对于thread 2. 延迟时间3后执行任务 3.单位TimeUnit.SECONDS 是 【秒】 三秒后执行
((ScheduledExecutorService) scheduledPool).schedule(task2,5,TimeUnit.SECONDS); //延迟5秒后执行任务 ((ScheduledExecutorService) scheduledPool).scheduleAtFixedRate(task3, 8, 3, TimeUnit.SECONDS); //对于task3 程序启动 8秒后执行,中间间隔3秒执行一次。 // scheduledPool.shutdown();
}
} class ScheduledThreadDemo extends Thread{
@Override
public void run() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(System.currentTimeMillis()));
System.out.println("我是线程1, 我在运行");
System.out.println("正在运行的线程名字" + Thread.currentThread().getName());
} }

查看结果

常见线程池 newScheduledThreadPool 定时执行任务的线程池 简单介绍的更多相关文章

  1. 9、java5线程池之定时任务线程池newScheduledThreadPool与newSingleThreadScheduledExecutor

    JDK文档描述 newSingleThreadScheduledExecutor() 创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行.(注意,如果因为在关闭前的执行期间出现失败而终 ...

  2. 常见线程池之 newCacheThreadPool 缓存线程池 简单使用

    package com.aaa.threaddemo; import java.util.concurrent.BlockingQueue; import java.util.concurrent.E ...

  3. java多线程系类:JUC线程池:01之线程池架构

    概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容--线程池.内容包括:线程池架构 ...

  4. 这么说吧,java线程池的实现原理其实很简单

    好处 : 线程是稀缺资源,如果被无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池对线程进行统一分配.调优和监控,有以下好处: 1.降低资源消耗: 2.提高响应速度: 3.提高线 ...

  5. 线程池系列一:线程池作用及Executors方法讲解

    线程池的作用: 线程池作用就是限制系统中执行线程的数量.     根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果:少了浪费了系统资源,多了造成系统拥挤效率不高.用线程池控制线程数量 ...

  6. python基础-12 多线程queue 线程交互event 线程锁 自定义线程池 进程 进程锁 进程池 进程交互数据资源共享

    Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...

  7. Android线程管理之ThreadPoolExecutor自定义线程池

    前言: 上篇主要介绍了使用线程池的好处以及ExecutorService接口,然后学习了通过Executors工厂类生成满足不同需求的简单线程池,但是有时候我们需要相对复杂的线程池的时候就需要我们自己 ...

  8. java多线程系类:JUC线程池:03之线程池原理(二)(转)

    概要 在前面一章"Java多线程系列--"JUC线程池"02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包 ...

  9. 从源码解读线程(Thread)和线程池(ThreadPoolExecutor)的状态

    线程是比进程更加轻量级的调度执行单位,理解线程是理解并发编程的不可或缺的一部分:而生产过程中不可能永远使用裸线程,需要线程池技术,线程池是管理和调度线程的资源池.因为前不久遇到了一个关于线程状态的问题 ...

随机推荐

  1. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  2. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  3. CRB and His Birthday(hdu 5410)

    CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. Codeforces 1073D:Berland Fair(模拟)

    time limit per test: 2 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: stand ...

  5. 【项目管理】《IT项目管理》Kathy Schwalbe 第2章 IT项目管理和IT背景

    1.对项目管理采取系统的观点有何意义?如何在项目管理中采用系统的观点? 意义:有效处理复杂的环境 采用系统方法,系统分析,系统管理.2.解释组织的四个框架.他们是如何帮助项目经理理解项目的组织环境的? ...

  6. 新手入门typeScript

    强类型与弱类型(类型安全) 强类型不允许随意的隐士类型转换,而弱类型是允许的 变量类型允许随时改变的特点,不是强弱类型的差异 静态类型与动态类型(类型检查) 静态类型:一个变量声明时它的类型就是明确的 ...

  7. 【优雅代码】03-optional杜绝空指针异常

    [优雅代码]03-optional杜绝空指针异常 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请顺 ...

  8. node.js安装及环境配置超详细教程【Windows系统安装包方式】

    文章目录 Step1:下载安装包 Step2:安装程序 Step3:查看 Step4:环境配置 最后补充: Step1:下载安装包 https://nodejs.org/zh-cn/download/ ...

  9. MyBatis 二级缓存实现详解及使用注意事项

    二级缓存介绍 在上文中提到的一级缓存中,其最大的共享范围就是一个SqlSession内部,如果多个SqlSession之间需要共享缓存,则需要使用到二级缓存.开启二级缓存后,会使用CachingExe ...

  10. springMVC+redis+redis自定义工具类 的配置

    1. maven项目,加入这一个依赖包即可, <dependency> <groupId>redis.clients</groupId> <artifactI ...