Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-以不同的时间间隔运行任务_Java_Executorservice_Threadpoolexecutor_Thread Sleep_Scheduledexecutorservice - Fatal编程技术网

Java-以不同的时间间隔运行任务

Java-以不同的时间间隔运行任务,java,executorservice,threadpoolexecutor,thread-sleep,scheduledexecutorservice,Java,Executorservice,Threadpoolexecutor,Thread Sleep,Scheduledexecutorservice,这个问题是关于大学作业的。 我希望每n*2秒运行一段代码(例如,等待1秒并运行,等待2秒并运行,等待4秒并运行,等等),最多运行5次 我现在有类似的东西 int计时器=1000; int tryCounter=0; 而(!condition()&计数器

这个问题是关于大学作业的。
我希望每n*2秒运行一段代码(例如,等待1秒并运行,等待2秒并运行,等待4秒并运行,等等),最多运行5次

我现在有类似的东西

<代码>int计时器=1000; int tryCounter=0; 而(!condition()&计数器<5){ doTask(); 睡眠(定时器); 计时器*=2; 计数器++; } 虽然这样做有效,但我的成绩得益于不使用
Thread.sleep()
。我发现使用固定速率的
ScheduledThreadPoolExecutor
是一种方法,但我无法让它工作,因为实际上间隔不是固定的

这适用于具有高并发能力的理论分布式系统,因此重要的是高可扩展性


如果把它写在我的报告中真的没有任何好处或可行的方法,我可以通过
Thread.sleep()
逃脱。有人对此有什么见解吗?

可以使用ScheduledExecutorService结合一些逻辑来安排任务。.schedule参数用于指定要使用的时间单位。您可以声明一个变量,该变量可以处理您尝试执行的增量

int timer = 1000;
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    Runnable runnable = new Runnable() {
        public void run()
        {
           //Move your code you want to implement here
        }
    };
    //Increment your variable
    while(!condition()) {
        for(int i = 0; i < 5; i++) {
            service.schedule(runnable, timer, TimeUnit.SECOND);
            timer *= 2;
        }
    }
int定时器=1000;
ScheduledExecutorService=Executors.newSingleThreadScheduledExecutor();
Runnable Runnable=新的Runnable(){
公开募捐
{
//将要在此处实现的代码移动到此处
}
};
//增加你的变量
而(!condition()){
对于(int i=0;i<5;i++){
服务。时间表(可运行、计时器、时间单位。秒);
计时器*=2;
}
}

在runnable块中移动代码执行,然后在计时器递增的for循环中对其进行调度,应该可以达到预期的效果。希望有帮助

可以使用ScheduledExecutorService和一些逻辑来调度任务。.schedule参数用于指定要使用的时间单位。您可以声明一个变量,该变量可以处理您尝试执行的增量

int timer = 1000;
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    Runnable runnable = new Runnable() {
        public void run()
        {
           //Move your code you want to implement here
        }
    };
    //Increment your variable
    while(!condition()) {
        for(int i = 0; i < 5; i++) {
            service.schedule(runnable, timer, TimeUnit.SECOND);
            timer *= 2;
        }
    }
int定时器=1000;
ScheduledExecutorService=Executors.newSingleThreadScheduledExecutor();
Runnable Runnable=新的Runnable(){
公开募捐
{
//将要在此处实现的代码移动到此处
}
};
//增加你的变量
而(!condition()){
对于(int i=0;i<5;i++){
服务。时间表(可运行、计时器、时间单位。秒);
计时器*=2;
}
}

在runnable块中移动代码执行,然后在计时器递增的for循环中对其进行调度,应该可以达到预期的效果。希望有帮助

与调用固定速率不同,您可能希望任务的每次运行计划下一次运行的延迟更长。与调用固定速率不同,您可能希望任务的每次运行计划下一次运行的延迟更长。此设计不回答
!condition()
是问题的一部分,每次都会启动所有线程。嗯。。。。通过将not条件嵌入while循环(如上所述),实现not条件非常简单。不清楚您的情况,但这与您提供的相符。至于不执行每个线程,我不确定您不希望执行的是什么,但您可以构建多个可运行块,甚至多个ScheduledExecutorServices,以分离调用调度程序时运行的内容。此设计不回答
!condition()
是问题的一部分,每次都会启动所有线程。嗯。。。。通过将not条件嵌入while循环(如上所述),实现not条件非常简单。不清楚您的情况,但这与您提供的相符。至于不执行每个线程,我不确定您不希望执行的是什么,但您可以构建多个可运行块,甚至多个ScheduledExecutorServices,以分离调用调度程序时运行的内容