- public class Task1 {
- public static void main(String[] args) {
-
- final long timeInterval = 1000;
- Runnable runnable = new Runnable() {
- public void run() {
- while (true) {
-
- System.out.println("Hello !!");
-
- try {
- Thread.sleep(timeInterval);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- Thread thread = new Thread(runnable);
- thread.start();
- }
- }
- import java.util.Timer;
- import java.util.TimerTask;
- public class Task2 {
- public static void main(String[] args) {
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
-
- System.out.println("Hello !!!");
- }
- };
- Timer timer = new Timer();
- long delay = 0;
- long intevalPeriod = 1 * 1000;
-
- timer.scheduleAtFixedRate(task, delay, intevalPeriod);
- timer.schedule(task, delay);
- }
- }
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class Task3 {
- public static void main(String[] args) {
- Runnable runnable = new Runnable() {
- public void run() {
-
- System.out.println("Hello !!");
- }
- };
- ScheduledExecutorService service = Executors
- .newSingleThreadScheduledExecutor();
-
- service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS);
- // 创建并执行在给定延迟后启用的 ScheduledFuture。
// 参数:
// callable - 要执行的功能
// delay - 从现在开始延迟执行的时间
// unit - 延迟参数的时间单位
// 返回:
// 可用于提取结果或取消的 ScheduledFuture
-
service.schedule(runnable, delay, TimeUnit.MILLISECONDS);
-
}
-
}
posted @
2018-08-22 14:21
玉龙小主
阅读(
715)
评论()
编辑
收藏
举报