Java 如何获得完全未来的结果

Java 如何获得完全未来的结果,java,future,executorservice,futuretask,completable-future,Java,Future,Executorservice,Futuretask,Completable Future,在Main类中,我尝试使用CompletableFuture异步运行任务。如FMSMsgHandlerSupplier类代码所示,返回Double[]类型的数据 问题是,在主类的for循环中,FMSMsgHandlerSupplier被调用了5次,让我们假设每次迭代我都会收到数据类型的新值 Double[],如何获得每次调用FMSMsgHandlerSupplier类后计算的结果 Main: public class Main { private final static String TAG

在Main类中,我尝试使用CompletableFuture异步运行任务。如FMSMsgHandlerSupplier类代码所示,返回Double[]类型的数据

问题是,在主类的for循环中,FMSMsgHandlerSupplier被调用了5次,让我们假设每次迭代我都会收到数据类型的新值 Double[],如何获得每次调用FMSMsgHandlerSupplier类后计算的结果

Main

public class Main {

private final static String TAG = Main.class.getSimpleName();

public static void main(String[] args) throws InterruptedException, ExecutionException {

    CompletableFuture<Double[]> compFuture = null;
    for (int i = 0; i < 5; i++) {
        compFuture = CompletableFuture.supplyAsync(new FMSMsgHandlerSupplier(1000 + (i*1000), "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12"));
    }
}
public class FMSMsgHandlerSupplier implements Supplier<Double[]>{

private final static String TAG = FMSMsgHandlerSupplier.class.getSimpleName();
private String mFMSMsg = ""; 
private static long sStartTime = TimeUtils.getTSMilli();
private int mSleepTime;

public FMSMsgHandlerSupplier(int sleepTime, String fmsMsg) {
    // TODO Auto-generated constructor stub
    this.mFMSMsg = fmsMsg;
    this.mSleepTime = sleepTime;
}

public Double[] get() {
    // TODO Auto-generated method stub
    if (this.mFMSMsg != null && !this.mFMSMsg.isEmpty()) {

        Double[] inputMeasurements = new Double[9];

        String[] fmsAsArray = this.toArray(this.mFMSMsg);
        inputMeasurements = this.getInputMeasurements(fmsAsArray);

        return inputMeasurements;

    } else {
        Log.e(TAG, "FMSMsgHandler", "fmsMsg is null or empty");

        return null;
    }
}
公共类主{
私有最终静态字符串标记=Main.class.getSimpleName();
公共静态void main(字符串[]args)引发InterruptedException、ExecutionException{
CompletableFuture compFuture=null;
对于(int i=0;i<5;i++){
compFuture=CompletableFuture.supplyAsync(新的FMSMGhandler供应商(1000+(i*1000),“1,2,3,4,5,6,7,8,9,10,11,12”);
}
}
}

FMSMsghandler供应商

public class Main {

private final static String TAG = Main.class.getSimpleName();

public static void main(String[] args) throws InterruptedException, ExecutionException {

    CompletableFuture<Double[]> compFuture = null;
    for (int i = 0; i < 5; i++) {
        compFuture = CompletableFuture.supplyAsync(new FMSMsgHandlerSupplier(1000 + (i*1000), "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12"));
    }
}
public class FMSMsgHandlerSupplier implements Supplier<Double[]>{

private final static String TAG = FMSMsgHandlerSupplier.class.getSimpleName();
private String mFMSMsg = ""; 
private static long sStartTime = TimeUtils.getTSMilli();
private int mSleepTime;

public FMSMsgHandlerSupplier(int sleepTime, String fmsMsg) {
    // TODO Auto-generated constructor stub
    this.mFMSMsg = fmsMsg;
    this.mSleepTime = sleepTime;
}

public Double[] get() {
    // TODO Auto-generated method stub
    if (this.mFMSMsg != null && !this.mFMSMsg.isEmpty()) {

        Double[] inputMeasurements = new Double[9];

        String[] fmsAsArray = this.toArray(this.mFMSMsg);
        inputMeasurements = this.getInputMeasurements(fmsAsArray);

        return inputMeasurements;

    } else {
        Log.e(TAG, "FMSMsgHandler", "fmsMsg is null or empty");

        return null;
    }
}
公共类FMSMsgHandlerSupplier实现供应商{
私有最终静态字符串标记=FMSMsgHandlerSupplier.class.getSimpleName();
私有字符串mFMSMsg=“”;
private static long sStartTime=TimeUtils.getTSMilli();
私密的国际时间;
公共fmsmsghandler供应商(int sleepTime,字符串fmsMsg){
//TODO自动生成的构造函数存根
this.mFMSMsg=fmmsg;
this.mSleepTime=睡眠时间;
}
公共双[]获取(){
//TODO自动生成的方法存根
if(this.mFMSMsg!=null&!this.mFMSMsg.isEmpty()){
Double[]inputMeasurements=新的双精度[9];
字符串[]fmsaArray=this.toArray(this.mFMSMsg);
inputMeasurements=此.getInputMeasurements(FmAsArray);
返回输入测量值;
}否则{
Log.e(标记“FMSMsgHandler”,“fmsMsg为null或空”);
返回null;
}
}
您可以使用completable future的get()方法来获取该值。但这意味着您的循环将等待该值返回。更合适的方法是使用Accept、Run…等方法之一。例如

public static void main(String[] args) {
    List<CompletableFuture> compFutures = new ArrayList<>();
    //timeout in seconds
    int TIMEOUT = 120; //or whatever
    for (int i = 0; i < 5; i++) {
    CompletableFuture<Double[]> compFuture = CompletableFuture.supplyAsync(new FMSMsgHandlerSupplier(1000 + (i*1000), "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12"));
        compFuture.thenAcceptAsync(d -> doSomethingWithDouble(d));
        compFutures.add(compFuture);    
    }
    CompletableFuture.allOf(compFutures).get(TIMEOUT, TimeUnit.SECONDS);
}

public static void doSomethingWithDouble(Double[] d) {
    System.out.print(d);
}
publicstaticvoidmain(字符串[]args){
List compFutures=new ArrayList();
//超时(秒)
int TIMEOUT=120;//或其他
对于(int i=0;i<5;i++){
CompletableFuture compFuture=CompletableFuture.supplyAsync(新的FMSMGhandler供应商(1000+(i*1000),“1,2,3,4,5,6,7,8,9,10,11,12”);
compFuture.theneptosync(d->doSomethingWithDouble(d));
compFutures.add(compFuture);
}
CompletableFuture.allOf(compFutures.get)(超时,时间单位.秒);
}
公共静态无效doSomethingWithDouble(Double[]d){
系统输出打印(d);
}

这看起来很明显,但只是以防万一--
compFuture.get()
?这样,
main
可能会在任何期货有机会运行完成之前退出。@acelent这是真的。谢谢你指出这一点。我已经更新了代码。