Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中尝试使用CompletableFuture类时,并没有返回输出,我搞砸了什么?_Java - Fatal编程技术网

在java中尝试使用CompletableFuture类时,并没有返回输出,我搞砸了什么?

在java中尝试使用CompletableFuture类时,并没有返回输出,我搞砸了什么?,java,Java,java和CompletableFuture类的新手。我的程序运行,但不向控制台返回任何内容。确信它是一个变量,没有被打印或分配到某个地方,但还没有弄清楚它。我做错了什么 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.concurrent.CompletableFuture; p

java和CompletableFuture类的新手。我的程序运行,但不向控制台返回任何内容。确信它是一个变量,没有被打印或分配到某个地方,但还没有弄清楚它。我做错了什么

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.concurrent.CompletableFuture;


public class AsyncMain {

    public String contents;

    public void process(String output) throws IOException {

        URL address = new URL("https://api.publicapis.org/random?category=animal");

        InputStreamReader reader = null;
        try {
            reader = new InputStreamReader(address.openStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader buffer = new BufferedReader(reader);

        //String contents = "";
        String line = "";

        while((line = buffer.readLine()) != null){
            if (line.isEmpty()) {
                break;
            }
            contents += line;
        }

        output = contents;
        System.out.println(output.toString());
    }

    public String address_3(String msg){
        try {
            this.process(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(msg);
        return msg;

    }

    public String address_2(String msg){
        try {
            this.process(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(msg);
        return msg;
    }

    public String address_1() {
        try {
            this.process(null);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void main(String args[]){
        AsyncMain as = new AsyncMain();
        CompletableFuture<String> cf = new CompletableFuture<>();

        cf.supplyAsync(as::address_1)
            .thenApply(as::address_2)
            .thenApply(as::address_3);
    }
}

导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.URL;
导入java.util.concurrent.CompletableFuture;
公共类AsyncMain{
公共字符串内容;
公共无效进程(字符串输出)引发IOException{
URL地址=新URL(“https://api.publicapis.org/random?category=animal");
InputStreamReader=null;
试一试{
reader=新的InputStreamReader(address.openStream());
}捕获(IOE异常){
e、 printStackTrace();
}
BufferedReader buffer=新的BufferedReader(读卡器);
//字符串内容=”;
字符串行=”;
而((line=buffer.readLine())!=null){
if(line.isEmpty()){
打破
}
内容+=行;
}
输出=内容;
System.out.println(output.toString());
}
公共字符串地址_3(字符串消息){
试一试{
本工艺(msg);
}捕获(IOE异常){
e、 printStackTrace();
}
System.out.println(msg);
返回味精;
}
公共字符串地址_2(字符串消息){
试一试{
本工艺(msg);
}捕获(IOE异常){
e、 printStackTrace();
}
System.out.println(msg);
返回味精;
}
公共字符串地址_1(){
试一试{
此进程(空);
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
公共静态void main(字符串参数[]){
AsyncMain as=新的AsyncMain();
CompletableFuture cf=新的CompletableFuture();
cf.supplyAsync(as::address_1)
.thenApply(as::address_2)
。然后应用(as::address_3);
}
}

我在这里尝试了独立于CompletableFuture尝试的函数“process”。它工作并从URL对象返回字符串值。但是当尝试与CompletableFuture类集成以从URL对象打印出2个值时。控制台上没有打印任何内容

这行代码返回一个
CompletableFuture

CompletableFuture<String> completableFuture =
        cf.supplyAsync(as::address_1).thenApply(as::address_2).thenApply(as::address_3);
CompletableFuture
定义可与其他步骤组合的异步计算步骤的约定。 您还可以通过
CompletableFuture.get()
method调用获得
CompletableFuture
的结果,该调用还将阻塞主线程,直到以后的步骤完成

CompletableFuture<String> completableFuture =
        cf.supplyAsync(as::address_1).thenApply(as::address_2).thenApply(as::address_3);
completableFuture.get();

CompletableFuture CompletableFuture=
cf.supplyAsync(as::address_1)。然后apply(as::address_2)。然后apply(as::address_3);
completableFuture.get();

应用程序很可能在异步代码完成之前退出。您正在使用公共
ForkJoinPool
,它使用守护进程线程。换句话说,您正在异步运行某些东西。这本质上意味着任务在后台运行,并且
main
继续正常运行。当然,一旦到达
main
的末尾,无论异步任务是否完成,程序都会退出。您可能需要查看
join
isDone
CompletableFuture<String> completableFuture =
        cf.supplyAsync(as::address_1).thenApply(as::address_2).thenApply(as::address_3);
completableFuture.get();