Java Executor Best Practices for Tasks that Should Run Forever

后端 未结 4 1240
不知归路
不知归路 2020-12-22 19:20

I\'m working on a Java project where I need to have multiple tasks running asynchronously. I\'m led to believe Executor is the best way for me to do this, so I\'m familiari

4条回答
  •  余生分开走
    2020-12-22 19:43

    how about this

    Runnable task = () -> {
      try{
        // do the task steps here
      } catch (Exception e){
        Thread.sleep (TIME_FOR_LONGER_BREAK);
      }    
    };
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(task,0, 0,TimeUnit.SECONDS);
    

提交回复
热议问题