目标:每5秒监视5个json URL,其中5个线程并行运行.
我想运行多个并行线程来监视JSON URL,每个时间间隔为n秒.我正在使用ScheduledExecutorService这个.
ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
for(NetworkBwXmlObject x : xmlDsList) {
executor.scheduleAtFixedRate(new processJsonUrl(x.getJsonUrl(),x.getId(), ctx), 0, 5, TimeUnit.SECONDS);
}
class processJsonUrl implements Runnable {
}
Run Code Online (Sandbox Code Playgroud)
这是创建5个线程来监控5个URL的正确方法.我不想在这里使用线程池.所有5个线程必须处于活动状态,直到应用程序的生命周期.
可以ScheduledExecutorService帮助我的方案或有另一种方法来实现这一目标?
谢谢
java concurrency multithreading executorservice scheduledexecutorservice
我有一个工作池,我定期使用ScheduledExecutorService帮助.现在我想停止定期执行某些任务,而其他任务应该不受影响.这个仪表的好方法是什么?
使用ScheduledExecutorService's scheduleAtFixedRate()定期运行一段代码而不是创建一个Runnable具有永久循环Thread.sleep()并且导致线程在所需时段内睡眠的新代码有什么好处?
使用其中一种方法可以获得性能提升吗?
java multithreading scheduled-tasks scheduledexecutorservice
我使用ScheduledExecutorService,我希望它每隔10秒进行一次计算一分钟,然后在那一分钟后给我返回新的值.我该怎么做?
示例:所以它收到5它增加+1六次然后它应该在一分钟后返回值11.
到目前为止,我没有工作的是:
package com.example.TaxiCabs;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.*;
public class WorkingWithTimeActivity {
public int myNr;
public WorkingWithTimeActivity(int nr){
myNr = nr;
}
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public int doMathForAMinute() {
final Runnable math = new Runnable() {
public void run() {
myNr++;
}
};
final ScheduledFuture<?> mathHandle =
scheduler.scheduleAtFixedRate(math, 10, 10, SECONDS);
scheduler.schedule(
new Runnable() {
public void run() {
mathHandle.cancel(true);
}
}, 60, SECONDS);
return myNr;
}
Run Code Online (Sandbox Code Playgroud)
}
并在我的主要活动中,我希望它在1分钟后将我的txtview文本更改为11;
WorkingWithTimeActivity test …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.err.println("Getting messages....");
getNewMessagesAndAddToTextArea();
}
}, 0, GET_MESSAGE_INTERVAL, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
exec在我的类中声明为:private ScheduledExecutorService exec;
我收到"获取消息......"消息2次然后停止.我无法解释原因.GUI仍然有效.那么这里发生了什么?
我当前正在创建一个新线程,以检查文件夹中是否有新文件,然后在定义的时间内休眠。
我的首选是使用ScheduledExecutorService,但是我找不到任何文档来阐明该服务在开始新任务之前是否等待当前正在运行的任务完成。
例如,如果我有以下代码;
Integer samplingInterval = 30;
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(10);
executorService.scheduleAtFixedRate(new WatchAgent(agentInfo), 0, samplingInterval, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
如果WatchAgent的run()花费的时间超过30秒,是否会在完成之前创建新的代理?
其次,如果创建WatchAgent的实例,是否可以在每次定期运行中继续使用相同的实例?
嗨我在互联网上阅读,我们可以使用报警管理器安排我们想要在一定间隔的间隙运行的任何事情ScheduledExecutorService.
我想知道它们与什么时候使用的区别
提前致谢.
我正在和一个人合作ScheduledExecutorService.我用这种方式:
ScheduledExecutorService executor;
public class ScheduledFanSpeedController implements Runnable {
.
.
.
public void start(){
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 0, 55, TimeUnit.SECONDS);
}
Run Code Online (Sandbox Code Playgroud)
该.scheduleAtFixRate可能抛出RejectedExecutionException.如果执行者在第n次执行任务时抛出此异常,我将如何捕获此异常?我是否真的不得不为此压倒一切?
我有一个来自 Oracle 站点的 Scheduled Executor Service 示例代码。它创建一个核心池大小为 1 的 ScheduledExecutorService。它执行 2 项工作:首先,它启动以固定时间间隔执行的重复任务,然后在延迟后终止同一任务和服务本身。
ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1);
//This will keep executing the task at fixed interval
ScheduledFuture<?> futureTask = scheduledService.scheduleAtFixedRate(new RepeatedTask(), initialDelay, interval, TimeUnit.SECONDS);
//A future task is returned which can be used to cancel the execution after sometime
//Here we will cancel our repeated task after 100 seconds
scheduledService.schedule(new TaskStopper(futureTask, scheduledService), 100, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
重复任务代码:
public class RepeatedTask implements Runnable{
int count = 0;
@Override
public void run() {
count++;
System.out.println(count …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ScheduledExecutorService 每 15 分钟调用一次可运行任务。这些任务应该在以下时间每小时运行一次:2nd Minute 10th Second 17th Minute 10th Second 32nd Minute 10th Second 47th Minute 10th Second。
我使用 Calendar 实例计算第一次执行任务的初始延迟,然后使用带有 scheduleAtFixedRate() 的延迟每 15 分钟执行一次任务。这在前几天工作正常,但几天后我可以观察到任务执行中的一些时间偏移。例如,原本应该在 12:02:10 开始的任务在 12:03:45 被调用。我哪里出错了?
public static void main(String[] args) {
final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(15);
Date aDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(aDate);
int currentHour = cal.get(Calendar.HOUR_OF_DAY);
int currentMins = cal.get(Calendar.MINUTE);
int currentSecs = cal.get(Calendar.SECOND);
int unitInSecs;
int delayFor15MinTasksInSecs;
unitInSecs = currentMins * 60 + currentSecs;
delayFor15MinTasksInSecs = unitInSecs < …Run Code Online (Sandbox Code Playgroud)