Java 定时器ScheduleExecutorService实战
2016-12-02 18:33
369 查看
package javatest.timetest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * Java 定时器ScheduleExecutorService实战 * Created by Lovell on 02/12/2016. */ /* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@ @@ @@ @@@@ @@@@ @@@@@@@@ @@@@ @@ @@@ @@@ @@@@ @@@ @@ @@@@ @@ @@@@ @@@ @@@@ @@@@ @@ @@@@ @@ @@@ @@@ @@@@ @@ @@ @@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ public class ScheduleExecutorServiceTest { private static Logger logger = LoggerFactory.getLogger(ScheduleExecutorServiceTest.class); /** * 获取指定时间对应的毫秒数 * * @param time "HH:mm:ss" * @return */ private static long getTimeMillis(String time) { try { DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd"); Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time); return curDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, * long initialDelay, * long period, * TimeUnit unit); * command:执行线程 * initialDelay:初始化延时 * period:两次开始执行最小间隔时间 * unit:计时单位 */ /** * 以固定延迟时间进行执行 */ public static void executeAtFixedRate() { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); executor.scheduleAtFixedRate( new Runnable() { @Override public void run() { System.out.println("executeAtFixedRate"); } }, 0, 100, TimeUnit.MILLISECONDS); } /** * public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, * long initialDelay, * long delay, * TimeUnit unit); * command:执行线程 * initialDelay:初始化延时 * period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间) * unit:计时单位 */ /** * 以固定延迟时间进行执行 * 本次任务执行完成后,需要延迟设定的延迟时间,才会执行新的任务 */ public static void executeWithFixedRate() { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); executor.scheduleWithFixedDelay( new Runnable() { @Override public void run() { System.out.println("executeWithFixedRate"); } }, 0, 100, TimeUnit.MILLISECONDS); } /** * scheduleAtFixedRate * 每天指定时间 * 每天定时安排任务进行执行 */ public static void executeAtTimePerDay() { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); long oneDay = 24 * 60 * 60 * 1000; long initDelay = getTimeMillis("18:27:00") - System.currentTimeMillis(); initDelay = initDelay > 0 ? initDelay : oneDay + initDelay; executor.scheduleAtFixedRate( new Runnable() { @Override public void run() { System.out.println("executeEightAtNightPerDay"); } }, initDelay, oneDay, TimeUnit.MILLISECONDS); } /** * 使用ScheduledExecutorService 延迟定时 */ private static long start; public static void executeDelayTime() { // 使用工厂方法初始化一个ScheduledThreadPool ScheduledExecutorService newScheduledThreadPool = Executors .newScheduledThreadPool(2); TimerTask task1 = new TimerTask() { @Override public void run() { try { System.out.println("task1 invoked ! " + (System.currentTimeMillis() - start)); Thread.sleep(3000); } catch (Exception e) { e.printStackTrace(); } } }; TimerTask task2 = new TimerTask() { @Override public void run() { System.out.println("task2 invoked ! " + (System.currentTimeMillis() - start)); } }; start = System.currentTimeMillis(); newScheduledThreadPool.schedule(task1,1000,TimeUnit.MILLISECONDS); newScheduledThreadPool.schedule(task2,3000,TimeUnit.MILLISECONDS); } public static void main(String[] args) { // executeAtFixedRate(); // executeWithFixedRate(); // executeAtTimePerDay(); executeDelayTime(); } }
相关文章推荐
- Spring单元测试加载properties文件
- Struts2文件上传
- Java 高并发八:NIO和AIO详解
- java 移除字符串前面的0 字符
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- 类型转换错误java.math.BigDecimal cannot be cast to java.lang.String
- Java中的类详解
- Java 实现导出excel表 POI
- 二叉树之线索链表
- macOS Sierra 安装+破解 myeclipse2015 教程
- Integer 的小知识
- Gradle学习笔记(三)java项目编译
- Spring beans架构--set注入
- Spring做面向切面功能时需要注意的异常
- zabbix监控Java 8080端口
- Java数组详解
- java native方法
- java实现RSA加密和解密
- 配置eclipse.ini、jvm参数配置说明、解决eclipse卡死问题、eclipse内存配置
- Android studio JDK和SDK路径设置