ScheduledExecutorService的主要区别scheduleAtFixedRate和scheduleWithFixedDelay方法是什么?
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("scheduleAtFixedRate: " + new Date());
}
}, 1, 3L , SECONDS);
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("scheduleWithFixedDelay: " + new Date());
}
}, 1, 3L , SECONDS);
Run Code Online (Sandbox Code Playgroud)
它们在同一时间打印完全相同,似乎它们以完全相同的间隔执行.
我正在使用Java,我想在我的应用程序中保持servlet不断运行,但我不知道怎么做.我的servlet有一个方法,它每天从数据库中提供用户的计数以及整个数据库中用户的总数.所以我想保持servlet不断运行.
multithreading servlets background-process java-ee scheduledexecutorservice
我每天凌晨5点都在尝试完成某项任务.所以我决定使用ScheduledExecutorService这个,但到目前为止,我已经看到了一些示例,显示了如何每隔几分钟运行一次任务.
而且我无法找到任何显示如何在早上的特定时间(早上5点)每天运行任务的例子,同时也考虑夏令时的事实 -
以下是我的代码,每15分钟运行一次 -
public class ScheduledTaskExample {
private final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
public void startScheduleTask() {
/**
* not using the taskHandle returned here, but it can be used to cancel
* the task, or check if it's done (for recurring tasks, that's not
* going to be very useful)
*/
final ScheduledFuture<?> taskHandle = scheduler.scheduleAtFixedRate(
new Runnable() {
public void run() {
try {
getDataFromDatabase();
}catch(Exception ex) {
ex.printStackTrace(); //or loggger would be …Run Code Online (Sandbox Code Playgroud) 我有一个ScheduledExecutorService时间周期性的几个不同的任务scheduleAtFixedRate(Runnable, INIT_DELAY, ACTION_DELAY, TimeUnit.SECONDS);
我也有一个Runnable与此调度程序不同的用法.当我想从调度程序中删除其中一个任务时,问题就开始了.
有没有办法做到这一点?
我是否正确使用一个调度程序执行不同的任务?实现这个的最佳方法是什么?
我需要让我的RSS Feed阅读器每隔10分钟检查一次新帖子,然后在有新帖子的情况下解析它们.我还需要每分钟都更新UI.
我从不同的来源阅读和听到了不同的东西.我目前的理解是我可以ScheduledThreadPoolExecutor用来制作两个预定的线程,其中一个需要Handler更新UI.我不确定这些类或者最有效的使用方法TimerTask.
我也很不确定在哪里制作这些的子类.一位朋友建议TimerTask在FeedParser课堂上扩展为内部课程以使其更简单.但是,要以这种方式实现它,我必须使用该run()方法TimerTask而不重写它,这意味着我不能简单地使用我需要的参数来运行需要的函数.
简而言之,为此安排任务的最佳方法是什么,我将在哪里实现这些?
android handler threadpool timertask scheduledexecutorservice
目标:每隔一段时间执行一次代码.
问题:在性能方面,是否存在显着差异:
while(true) {
execute();
Thread.sleep(10 * 1000);
}
Run Code Online (Sandbox Code Playgroud)
和
executor.scheduleWithFixedDelay(runnableWithoutSleep, 0, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
?
当然,后一种选择更犹豫不决.然而,我想知道我是否应该开始一项名为"花几天时间重构遗留代码以告别Thread.sleep()"的冒险.
更新:此代码在超级/超级/超高负载环境中运行.
我想延迟做一些事情,就像设置倒计时器一样,在一段时间后"做一件事".
我希望我的其余程序在我等待的时候继续运行,所以我尝试自己Thread创建一个包含一分钟延迟的程序:
public class Scratch {
private static boolean outOfTime = false;
public static void main(String[] args) {
Thread countdown = new Thread() {
@Override
public void run() {
try {
// wait a while
System.out.println("Starting one-minute countdown now...");
Thread.sleep(60 * 1000);
// do the thing
outOfTime = true;
System.out.println("Out of time!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
countdown.start();
while (!outOfTime) {
try {
Thread.sleep(1000);
System.out.println("do other stuff here");
} catch (InterruptedException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) Code Review聊天中的讨论确定了ScheduledExecutorService的以下行为:
计划运行的任务失败并出现"严重"问题,但没有问题的报告,异常或日志.在其他情况下,应用程序通常会以错误终止.但是,在ScheduledExecutorService的上下文中,根本没有异常/错误"处理".
首先,要制造一个问题.以下类具有保证失败的静态初始化程序:
public class InitializerFault {
private static final int value = Integer.parseInt("fubar");
@Override
public String toString() {
return "" + value;
}
}
Run Code Online (Sandbox Code Playgroud)
运行时:
public static void main(String[] args) {
System.out.println(new InitializerFault());
}
Run Code Online (Sandbox Code Playgroud)
它产生(这正是我所期望的):
Exception in thread "main" java.lang.ExceptionInInitializerError
at SimpleHandler.main(SimpleHandler.java:5)
Caused by: java.lang.NumberFormatException: For input string: "fubar"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at InitializerFault.<clinit>(InitializerFault.java:4)
... 1 more
Run Code Online (Sandbox Code Playgroud)
但是,当运行时:
private static final Thread buildThread(Runnable r) {
Thread t = new Thread(r, "TestThread");
t.setDaemon(true);
System.out.println("Built thread …Run Code Online (Sandbox Code Playgroud) 我有一个蓝牙聊天示例应用程序的修改版本.我已经设置了一个ScheduledExecutorService使用以预定义的速率通过蓝牙发送命令scheduleAtFixedRate.
我已经设置了一个PreferenceActivity允许用户修改时间段.但我不确定如何在更新期间实现重复的实际任务.我需要以ScheduledExecutorService某种方式取消并重新启动吗?
这是我的代码的相关部分.
private ScheduledExecutorService scheduleTaskExecutor;
public long ReadInterval = 1;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
...
// This schedule a task to run every 1 second:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// If you need update UI, simply do this:
runOnUiThread(new Runnable() {
public void run() {
// update your UI component here.
if (connected == true) {
sendMessage("READ");
if (D) Log.i(TAG, "In …Run Code Online (Sandbox Code Playgroud)