Timer和ScheduledExecutorService区别

Timer里面的任务如果执行时间太长,会独占Timer对象,使得后面的任务无法几时的执行
ScheduledExecutorService不会出现Timer的问题(除非你只搞一个单线程池的任务区)


Timer搞了一个最小堆,每次取距离当前时间最近的那个任务来执行,
创建Timer的时间会创建TimerThread做为执行线程,所以一个Timer对应一个线程
,一个线程当然不能同时执行多个任务啦(当某个任务执行时间很长就看的出来)。


public Timer(String name, boolean isDaemon) {
thread.setName(name);
thread.setDaemon(isDaemon);
thread.start();
}

[color=darkred]当你创建一个Timer的时候就创建了一个执行任务的线程对象,不管你有多少个任务要执行,都只有这一个线程来负责执行任务,它每次从最小堆中取距离当前时间最短的任务来执行,所以如果某个任务非常耗时,那这是不明智的。所以建议这样的情况使用jdk1.5提供的线程池任务管理器。[/color]

class TimerThread extends Thread{}


Timer的任务队列是个最小堆java.util.TaskQueue



package thread.timer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo1 {
public static void main(String[] args) throws ParseException, InterruptedException {
/*
* Timer里面的任务如果执行时间太长,会独占Timer对象,使得后面的任务无法几时的执行
* ScheduledExecutorService不会出现Timer的问题(除非你只搞一个单线程池的任务区)
*
* Timer搞了一个最小堆,每次取距离当前时间最近的那个任务来执行,
* 创建Timer的时间会创建TimerThread做为执行线程,所以一个Timer对应一个线程
* ,一个线程当然不能同时执行多个任务啦(当某个任务执行时间很长就看的出来)。
* ScheduledExecutorService创建的线程数量是池子的大小,所以不会出现Timer那样的问题
*/
// ScheduledExecutorServiceDemo();
// timerDemo();

// Date df = DateFormat.getDateTimeInstance().parse("2011-10-10");
// System.out.println(df.);
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String s = sdf.format(new Date());
// System.out.println(s);
// convert(s, "yyyy-MM-dd HH:mm:ss");

// say();

ScheduledExecutorService pool = Executors
.newSingleThreadScheduledExecutor();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// pool.scheduleAtFixedRate(new Runnable() {
// public void run() {
// System.out.println(sdf.format(new Date()));
// }
// },0, 1000,TimeUnit.MILLISECONDS);
// pool.schedule(new Runnable() {
// public void run() {
// System.out.println(sdf.format(new Date()));
// }
// }, 1000, TimeUnit.MILLISECONDS);
// pool.shutdown();
Date d1 = new Date();
Date d2 = new Date();
d2.setTime(d2.getTime()-1);
System.out.println(d1.getTime());
System.out.println(d2.getTime());
System.out.println(d1.equals(d2));
System.out.println(d1.compareTo(d2));
}

private static long convert(String time, String format)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
// Date sdf = DateFormat.getDateTimeInstance().parse(time);
long millionSeconds = sdf.parse(time).getTime();
return millionSeconds;
// System.out.println(System.currentTimeMillis());
// System.out.println(millionSeconds);
}

private static void say() throws ParseException {
ScheduledExecutorService pool = Executors
.newSingleThreadScheduledExecutor();
long start = System.currentTimeMillis();
System.out.println("当前时间:"+start);
long deply = convert("2011-10-09 15:49:00", "yyyy-MM-dd HH:mm:ss")-start;
System.out.println(deply+"秒后执行");
// System.out.println(86268719/3600/1000);
pool.schedule(new Runnable() {
public void run() {
while (true) {
System.out.println("1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, deply, TimeUnit.MILLISECONDS);
}

private static void ScheduledExecutorServiceDemo() {
ScheduledExecutorService pool = Executors
.newSingleThreadScheduledExecutor();
pool.schedule(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1");
break;
}
}
}, 1000, TimeUnit.MILLISECONDS);
pool.schedule(new Runnable() {
public void run() {
System.out.println("2");
}
}, 3000, TimeUnit.MILLISECONDS);
}

private static void timerDemo() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1");
}
}
}, 100, 2000);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("2");
}
}, 1000, 1000);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值