public int getChildrenCount(int groupPosition) {
if(children == null){
new SalesRequest().execute(); // runs in other thread which
// initialises children with some value.
while(children == null){
// I'm doin this to avoid null pointer Exception.
// So it comes out of the loop only when childern
// gets initialised.
}
}
return children.length;
}
但我对自己处理此事的方式不满意。有更好的方法吗?
最佳答案
您可以使用 CountDownLatch 来等待其他线程完成。
http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/CountDownLatch.html
关于java - 我如何避免这种忙等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8207905/