- package com.jelly.timer;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- public class ScheduledExecutorServiceExample {
- public static void main(String[] args) {
- ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(2);
- executor.scheduleWithFixedDelay(new Task("51CTO博客.."), 0, 5, TimeUnit.SECONDS);
- executor.scheduleWithFixedDelay(new Task("你值得一览.."), 20, 5, TimeUnit.SECONDS);// 延迟20秒后再执行
- }
- static class Task implements Runnable {
- /**
- * run
- */
- public void run() {
- System.out.println(message);
- }
- public Task(String message) {
- this.message = message;
- }
- public final String message;
- }
- }
本文出自 “简单的jelly” 博客,请务必保留此出处http://jelly8090.blog.51cto.com/6153039/1036030