【问题标题】:Executing a method every x seconds with different parameters inside a loop in JAVA在JAVA中的循环内每x秒使用不同的参数执行一个方法
【发布时间】:2015-06-13 04:33:28
【问题描述】:

如何在循环内周期性地运行带有不同参数的方法?

Iteration 1 : obj.process(index1)
wait 5 seconds...
Iteration 2: obj.process(index2)
wait 5 seconds...
Iteration 3: obj.process(index3)
wait 5 seconds...
and so on...

具体来说,我的目标是重复运行一个方法,下一次迭代需要等待X秒,下一次迭代也需要等待X秒等等。

我的示例代码,它是错误的,几乎同时启动所有 obj.process(index)

Timer time = new Timer();           
for (final String index : indeces) {
        int counter = 0;            
        time.schedule(new TimerTask() {

            @Override
            public void run() {
                indexMap.put(index, obj.process(index));
            }
        }, delay);
        counter++;
        if (counter > indeces.size())
            System.exit(0);
    }

【问题讨论】:

  • 所以你想迭代一个循环假设 10 次,每次你想传递不同的参数并等待几秒钟。你想要吗??

标签: java scheduled-tasks timertask


【解决方案1】:

对所有对象使用一个Timer。使run 方法处理单个对象。如果有更多对象要处理,让 run 方法重新调度 TimerTask(即 this)。

这样的解决方案你不需要循环,定时器只需要设置一次。

【讨论】:

    【解决方案2】:

    这似乎对我有用:

    public class TestClass {
    
        static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {
    
            String [] indeces = new String[] {"one", "two", "three"};
    
            long delay = 0;
    
    
    
            Timer time = new Timer();           
            for (final String index : indeces) {
                    int counter = 0;        
                    System.out.println("index-->" + delay);
                    time.schedule(new TimerTask() {
    
                        Map indexMap = new HashMap();
    
                        @Override
                        public void run() {
                            indexMap.put(index, index);
                            System.out.println("index-->" + index);
    
                        }
                    }, delay);
                    counter++;
                    delay = delay + 5000; // Increase delay by 5 secs
                    if (counter > indeces.length)
                        System.exit(0);
                }       
    
        }
    

    基本上我为每个定时器任务增加了 5 秒的延迟

    【讨论】:

      【解决方案3】:

      如果您的代码在其自己的线程中运行,则以下最小示例可能很有用:

      public static void main(String[] args) {
          Object[][] parameters ={ new String[]{"HELLO", "WORLD"},
                                   new Integer[]{1,2,3},
                                   new Double[]{0.1, 0.9, 5.3}
                                  };
          for (int i = 0; i < parameters.length; i++) {
              try {
                  TimeUnit.SECONDS.sleep(1);
      
                  doSomething(parameters[i]);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
      
      private static void doSomething(Object[] objects) {
          System.out.println(Arrays.toString(objects));
      }
      

      在 Java 8 中,可能的解决方案是:

      public static void main(String[] args) {
          Object[][] parameters ={ new String[]{"HELLO", "WORLD"},
                                   new Integer[]{1,2,3},
                                   new Double[]{0.1, 0.9, 5.3}
                                  };
          Arrays.stream(parameters).forEachOrdered(p -> doSomething(p));
      }
      
      private static void doSomething(Object[] objects) {
          try {
              TimeUnit.SECONDS.sleep(1);
      
              System.out.println(Arrays.toString(objects));
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        • 2018-05-07
        • 2021-12-30
        • 1970-01-01
        • 1970-01-01
        • 2016-01-01
        • 2023-03-11
        相关资源
        最近更新 更多