如题所述
第1个回答 2022-03-15
import java.util.concurrent.Callable; public class CallableUse { public static void main(String... args) { // final int num = 100770; or int num = 100770; // 使用匿名的内部类, 如果需要传递参数可能需要将变量转换成final: try { callMethod(100, new Callable<Integer>() { public Integer call() { return needOperation(num); } }); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public static int needOperation(int param) { // do something param = 999; return param; } public static void callMethod(int i, Callable<Integer> myFunc) { // do something try { System.out.println(myFunc.call() ); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }