Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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/4/wpf/14.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 什么会导致类#newInstance在终止前阻塞线程?_Java_Class_Asynchronous_Reflection - Fatal编程技术网

Java 什么会导致类#newInstance在终止前阻塞线程?

Java 什么会导致类#newInstance在终止前阻塞线程?,java,class,asynchronous,reflection,Java,Class,Asynchronous,Reflection,我在Java中遇到了一个问题,即不能使用反射实例化泛型类并阻止它正在运行的CompletableFuture异步线程。如果不抛出任何异常,我不确定是什么原因导致了这种情况 我试图实例化的类有一个单一的、公共的、空白的构造函数(通常不指定任何构造函数): 反射代码: public static <T extends InfoCore> CompletableFuture<T> get(Class<T> infoClass, PlayerInfo play

我在Java中遇到了一个问题,即不能使用反射实例化泛型类并阻止它正在运行的CompletableFuture异步线程。如果不抛出任何异常,我不确定是什么原因导致了这种情况

我试图实例化的类有一个单一的、公共的、空白的构造函数(通常不指定任何构造函数):

反射代码:

    public static <T extends InfoCore> CompletableFuture<T> get(Class<T> infoClass, PlayerInfo playerInfo) {
        System.out.println("Getting an Info Core"); // TODO
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("Supplying the info core async thread"); // TODO
            try {
                System.out.println("Trying"); // TODO
                T instance = infoClass.newInstance();
                System.out.println("Got new instance"); // TODO
                instance.setPlayerInfo(playerInfo);
                System.out.println("Set player info in the instance"); // TODO
                instance.load();
                System.out.println("Loaded the instance"); // TODO
                return instance;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Error occurred instantiating an InfoCore");
            }
        });
    }
publicstaticcompletablefuture-get(Class-infoClass,playernfo-playernfo){
System.out.println(“获取信息核心”);//待办事项
返回CompletableFuture.SupplySync(()->{
System.out.println(“提供信息核心异步线程”);//TODO
试一试{
System.out.println(“Trying”);//待办事项
T instance=infoClass.newInstance();
System.out.println(“获得新实例”);//TODO
setPlayerInfo(playerInfo);
System.out.println(“在实例中设置播放器信息”);//TODO
load();
System.out.println(“加载实例”);//TODO
返回实例;
}捕获(例外e){
e、 printStackTrace();
抛出新的RuntimeException(“实例化InfoCore时出错”);
}
});
}
在“尝试”之前,我会收到所有System.out消息。“Get new instance”消息从未打印,整个异步线程无限期冻结(每次我都必须终止该进程)。我还尝试使用类#getConstructor(新类[0]).newInstance()获得相同的结果


是否有人知道这可能会发生什么?

在<构造器外初始化>代码> UserInfo <代码>中有哪些字段,你可能想考虑使用<代码>供应商>代码>而不是<代码>类< /代码>。还有其他字段,但它们没有初始化它们,它们是由SETTER方法初始化的。(例如代码中的#setplayerfo(playerfo))
UserInfo
的构造函数将调用超级构造函数。此外,如果这是您第一次实例化它,它可能会触发类初始化,执行任何静态初始值设定项,如果尚未初始化,也会触发任何超类的初始化以及使用默认方法实现的接口。Thi说到这里,这是最合理的场景,您从类初始值设定项调用此方法并等待完成,从而导致死锁。
    public static <T extends InfoCore> CompletableFuture<T> get(Class<T> infoClass, PlayerInfo playerInfo) {
        System.out.println("Getting an Info Core"); // TODO
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("Supplying the info core async thread"); // TODO
            try {
                System.out.println("Trying"); // TODO
                T instance = infoClass.newInstance();
                System.out.println("Got new instance"); // TODO
                instance.setPlayerInfo(playerInfo);
                System.out.println("Set player info in the instance"); // TODO
                instance.load();
                System.out.println("Loaded the instance"); // TODO
                return instance;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Error occurred instantiating an InfoCore");
            }
        });
    }