Java 是否可以使用ExecutorService计划一个可调用服务以0.5秒的延迟运行20次,并且;“早休息”;如果完成了? ResponsePOJO response=newresponsepojo(); 对于(int i=0;i{ System.out.printf(“%2d.%s%n”,(count+1),LocalTime.now(); 如果(++计数>20){ 已完成=新的原子布尔(); System.out.println(“假”); } 否则{ int anInt=rand.nextInt(10); System.out.println(“anInt=“+anInt”); if(anInt==val){ 已完成=新原子布尔值(true); System.out.println(“真”); } } }; ScheduledFuture sf=executor.scheduleWithFixedDelay(命令,5,1,时间单位:秒); while(已完成==null){ //什么也不做。 } sf.取消(真); executor.shutdown(); 系统输出打印项次(已完成); } }

Java 是否可以使用ExecutorService计划一个可调用服务以0.5秒的延迟运行20次,并且;“早休息”;如果完成了? ResponsePOJO response=newresponsepojo(); 对于(int i=0;i{ System.out.printf(“%2d.%s%n”,(count+1),LocalTime.now(); 如果(++计数>20){ 已完成=新的原子布尔(); System.out.println(“假”); } 否则{ int anInt=rand.nextInt(10); System.out.println(“anInt=“+anInt”); if(anInt==val){ 已完成=新原子布尔值(true); System.out.println(“真”); } } }; ScheduledFuture sf=executor.scheduleWithFixedDelay(命令,5,1,时间单位:秒); while(已完成==null){ //什么也不做。 } sf.取消(真); executor.shutdown(); 系统输出打印项次(已完成); } },java,asynchronous,executorservice,scheduledexecutorservice,Java,Asynchronous,Executorservice,Scheduledexecutorservice,有没有更好的方法通过Executor服务来实现这一点 我正试着和你一起做 ResponsePOJO response = new ResponsePOJO(); for (int i = 0; i < 20; i++) { response = processResponse(client.execute(get)); if (response.getStatus().equalsIgnoreCase("completed")) { b

有没有更好的方法通过Executor服务来实现这一点

我正试着和你一起做

ResponsePOJO response = new ResponsePOJO();

for (int i = 0; i < 20; i++) {
    response = processResponse(client.execute(get));
    if (response.getStatus().equalsIgnoreCase("completed")) {
        break;
    }
    else { 
        Thread.sleep(500);
    }
}

return response;
ScheduledExecutorService executor=执行者
.newSingleThreadScheduledExecutor();
Future-resultFuture=executor.scheduleAtFixedRate(runnableTask,1500,TimeUnit.ms);
然而,这有几个问题

  • 我无法在结果可用时立即返回
  • 我无法设置希望它执行的次数

  • 想知道是否有更好的方法来实现这一点吗?

    我从未使用过
    ScheduledExecutorService
    ,但基于此,我想出了以下代码。我想这可能不是最好的代码,因为这是第一次尝试。尽管如此,它还是完成了任务。代码后面的解释

    import java.time.LocalTime;
    导入java.util.Random;
    导入java.util.concurrent.Executors;
    导入java.util.concurrent.ScheduledExecutorService;
    导入java.util.concurrent.ScheduledFuture;
    导入java.util.concurrent.TimeUnit;
    导入java.util.concurrent.AtomicBoolean;
    公共类调度{
    静态挥发原子布尔完成;
    静态整数计数;
    静态int-val;
    静态随机兰德;
    公共静态void main(字符串[]args){
    rand=新随机数();
    val=兰特·耐克斯汀(10);
    System.out.println(“val=”+val);
    ScheduledExecutorService executor=Executors.newSingleThreadScheduledExecutor();
    可运行命令=()->{
    System.out.printf(“%2d.%s%n”,(count+1),LocalTime.now();
    如果(++计数>20){
    已完成=新的原子布尔();
    System.out.println(“假”);
    }
    否则{
    int anInt=rand.nextInt(10);
    System.out.println(“anInt=“+anInt”);
    if(anInt==val){
    已完成=新原子布尔值(true);
    System.out.println(“真”);
    }
    }
    };
    ScheduledFuture sf=executor.scheduleWithFixedDelay(命令,5,1,时间单位:秒);
    while(已完成==null){
    //什么也不做。
    }
    sf.取消(真);
    executor.shutdown();
    系统输出打印项次(已完成);
    }
    }
    
    基本上你想尝试一些东西,最多20次。一旦它成功了,你就不想再尝试了,如果20次尝试都没有成功,你也不想再尝试了。您还需要在连续尝试之间等待半秒钟。因为我不知道“某物”是什么,所以我编造了一些东西。每次尝试时,我都会生成一个介于0和10之间的新随机整数,并将其与某个预定值进行比较。如果数字匹配,则“某物”已成功


    我将详细信息打印到屏幕上纯粹是为了调试。他们不需要打印报表。您可以安全地删除它们。

    这与此类似,但并不完全相同。我希望Runnable总共运行20次。And@DonCode你的意思是你想让它一直运行20次吗?我想最多运行20次。但是如果我得到响应,我想提前停止@abra@DonCode这不是我在回答中写的吗?一旦它成功了,你就不想再尝试了,如果20次尝试都没有成功,你也不想再尝试了
    ScheduledExecutorService executor = Executors
      .newSingleThreadScheduledExecutor();
    
    Future<String> resultFuture = executor.scheduleAtFixedRate(runnableTask, 1, 500, TimeUnit.MILLISECONDS);