gpt4 book ai didi

java - 从构造函数调用阻塞方法 - Java

转载 作者:行者123 更新时间:2023-11-30 11:31:01 25 4
gpt4 key购买 nike

我想知道从构造函数本身内部调用阻塞方法是否是个坏主意。

我很想知道在这种情况下我们是否有任何指导方针/规则,我们不应该在构造函数中调用阻塞方法。

基本上我正在尝试这样做:

class A
{
// Many instance variables that is to be initialized on instantiaion
String tempVar = null;

public A()
{
// Initialize all the instance variables
tempVar=objectClassB.**callBlockingMethod**(); // this method call would return
// some data from ClassB Object
}

public static void main(String args ...)
{
A a = new A();
// Or should I call the blocking method call only after instantiation according
// to any guidelines of Java pertaining to performance ?

// IMPORTANT: It's only when the blocked method returns value , should the main
// thread proceed as the object 'a' would be sent for further processing
}
}

PS:嗯,如果我的问题听起来很基础,我很抱歉。

最佳答案

我认为最好在类 A 中创建一个方法,例如 connect ()。创建对象后,您可以像这样调用

A = 新 A()A.connect()

在连接方法中定义阻塞方法 StreamConnection con=notifier.acceptAndOpen().....

如果你的阻塞调用没有在指定的时间段内返回,你会考虑一些机制来恢复这种情况

关于java - 从构造函数调用阻塞方法 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17433656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com