1.Timer的缺陷
a)Timer在多任务情况下,如果任务(TimerTask)的执行时间超过任务间隔时间时,则需要等待前任务执行完成才会执行后任务
b)Timer在多任务情况下,若有任务出现异常的时候,则不会执行以下任务
以下为事例代码:
a <Demo>
public class Main3Activity extends AppCompatActivity {
private Timer timer;
private static final String TAG = "Main3Activity";
private static long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
timer = new Timer();
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask2 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
}
};
startTime = System.currentTimeMillis();
timer.schedule(timerTask1, 1000);
timer.schedule(timerTask2, 3000);
}
}
打印的log信息如下:
06-08 14:03:06.480 14116-14184/com.tago.lable.testfirst E/Main3Activity: run: 1000
06-08 14:03:10.482 14116-14184/com.tago.lable.testfirst E/Main3Activity: run: 5002
b <Demo>
public class Main3Activity extends AppCompatActivity {
private Timer timer;
private static final String TAG = "Main3Activity";
private static long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
timer = new Timer();
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
throw new RuntimeException();
}
};
TimerTask timerTask2 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
}
};
startTime = System.currentTimeMillis();
timer.schedule(timerTask1, 1000);
timer.schedule(timerTask2, 3000);
}
}
程序在打印出第一个TimerTask输出语句之后,直接崩溃。
2.ScheduledExecutorService用法
a <Demo> 多任务执行顺序
public class Main3Activity extends AppCompatActivity {
private static final String TAG = "Main3Activity";
private static long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask2 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
}
};
startTime = System.currentTimeMillis();
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2);
scheduledExecutorService.schedule(timerTask1, 1000, TimeUnit.MILLISECONDS);
scheduledExecutorService.schedule(timerTask2, 3000, TimeUnit.MILLISECONDS);
}
}
log
06-08 14:12:09.115 16092-16145/com.tago.lable.testfirst E/Main3Activity: run: 1002
06-08 14:12:11.116 16092-16146/com.tago.lable.testfirst E/Main3Activity: run: 3003
b <Demo> 多任务下前任务出现异常
public class Main3Activity extends AppCompatActivity {
private static final String TAG = "Main3Activity";
private static long startTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
throw new RuntimeException();
}
};
TimerTask timerTask2 = new TimerTask() {
@Override
public void run() {
Log.e(TAG, "run: " + (System.currentTimeMillis() - startTime));
}
};
startTime = System.currentTimeMillis();
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2);
scheduledExecutorService.schedule(timerTask1, 1000, TimeUnit.MILLISECONDS);
scheduledExecutorService.schedule(timerTask2, 3000, TimeUnit.MILLISECONDS);
}
}
不影响程序运行
log
06-08 14:14:11.745 16563-16590/com.tago.lable.testfirst E/Main3Activity: run: 1001
06-08 14:14:13.745 16563-16591/com.tago.lable.testfirst E/Main3Activity: run: 3001
构造参数说明:
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(2);
官方说明:
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
ScheduledExecutorService本事一个接口,我们在使用需要创建一个他的实现类。这个构造要求我们传入一个int值,这个值为 ScheduledThreadPoolExecutor创建的线程池的大小。
使用方法说明:
scheduledExecutorService.schedule(timerTask1, 1000, TimeUnit.MILLISECONDS);
第一个参数为执行的操作,第二参数为执行时间,第三个参数为执行时间的单位 /**
* Time unit representing one thousandth of a microsecond.
* 微秒
*/
NANOSECONDS
/**
* Time unit representing one thousandth of a millisecond.
* 毫秒
*/
MICROSECONDS
/**
* Time unit representing one thousandth of a second.
* 秒
*/
MILLISECONDS
/**
* Time unit representing one second.
*/
SECONDS
/**
* Time unit representing sixty seconds.
* 60 秒(分)
* @since 1.6
*/
MINUTES
/**
* Time unit representing sixty minutes.
* 60 分 (小時)
* @since 1.6
*/
HOURS
/**
* Time unit representing twenty four hours.
* 24 小时(天)
* @since 1.6
*/
DAYS