简体   繁体   English

如何在java中将函数作为参数传递以多次运行它

[英]How to pass a function as parameter in java to run it multiple times

Sorry to ask that question I've seen that it has been asked a lot but not the way I'm think it.很抱歉问这个问题,我看到有人问了很多,但不是我想的那样。

My goal is to make a retry function that would loop 'n' amount of time to try to run the method I'm passing as parameter.我的目标是创建一个重试函数,该函数将循环 'n' 时间以尝试运行我作为参数传递的方法。

So in this case I want to run the function "myFunction()" but I know that sometimes the returned value is false and I want to run it until the return value is true.所以在这种情况下,我想运行函数“myFunction()”,但我知道有时返回值是假的,我想运行它直到返回值是真。

That's how I would like it to run :这就是我希望它运行的方式:

public static void main (String [] args){
    boolean did work = retry (myFunction(),5)
    if(didwork){
        System.out.println("I found my stuff");
    } else {
        System.out.println("I didn't found my stuff");
    }
}

public boolean retry (Function function, int loopTry){
    boolean success = false;
    for(int i=0 ; i<loopTry ; i++){
        success = function.run();
        if(success){
            break;
        }
    }
    return success;
}

public boolean myFunction(){
    boolean found = false;
    //do stuff
    if(stuff){
        found = true;
    }
    return found;
}

I know that it's not possible (at least in java 7 maybe in 8)我知道这是不可能的(至少在 java 7 中可能在 8 中)

I've seen some answers suggesting to create an object for the specific function etc ... but that's wouldn't make the retry function generic since I would need to remake an object for each method that I would like to retry and that's not what I want.我已经看到一些答案建议为特定函数等创建一个对象......但这不会使重试函数通用,因为我需要为我想重试的每个方法重新创建一个对象,这不是什么我想要。

So I would like to know if there is a "clean" way to do it (even in java 8) or not ?所以我想知道是否有一种“干净”的方式来做到这一点(即使在 java 8 中)?

Thank you谢谢

You could try reflection here:你可以在这里尝试reflection

Eg:例如:

public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException {

  Method method = Test.class.getMethod("myFunction");

  boolean didwork = retry(method, 5);
  if (didwork) {
      System.out.println("I found my stuff");
  } else {
      System.out.println("I didn't found my stuff");
  }

}

public static boolean retry(Method method, int loopTry)
        throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

  boolean success = false;
  for (int i = 0; i < loopTry; i++) {
    success = (Boolean) method.invoke(null);
    if (success) {
      break;
    }
  }
  return success;
}

public static boolean myFunction() {
  boolean found = false;

  //do stuff
  if(stuff){
     found = true;
  }

  return found;
}

You could just add a while loop to myFunction() which "retries" inside the function.您可以向 myFunction() 添加一个 while 循环,该循环在函数内部“重试”。 This would be just one addtional line.这将只是一个额外的行。 Maximum two or three if you define the maximum number of tries as n.如果将最大尝试次数定义为 n,则最多为 2 或 3。 In the second case you've recreated the effect of either trying until you get a true or until the number if tries is reached.在第二种情况下,您重新创建了尝试直到获得 true 或直到达到尝试次数为止的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM