我想知道从构造器本身调用阻塞方法是不是一个坏主意。
我很想知道在这种情况下,我们是否有任何准则/规则,即我们永远不应该在构造器中调用阻塞方法。
基本上,我正在尝试这样做:
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
}
}
附言:嗯,如果我的问题听起来很简单,我很抱歉。
发布于 2013-07-02 19:41:14
我认为更好的方法是在类A中创建一个像connect ()这样的方法。在创建对象后,可以像这样调用
A a= new A() A.connect()
在connect方法中,您定义了阻塞方法StreamConnection con=notifier.acceptAndOpen() .....
如果您的阻塞调用在指定的时间段内没有返回,您可以考虑一些机制来恢复这种情况
https://stackoverflow.com/questions/17433656
复制