我的代码:
private AtomicBoolean fetched1 = new AtomicBoolean(false);
private int rowCount;
public int getRowCount() {
data.getRealm().exec(new Runnable(){
@Override
public void run() {
rowCount = data.size();
fetched1.set(true);
}
});
while(!fetched1.get()){
}
fetched1.set(false);
return rowCount;
}
现在看起来对我有用,但是我不熟悉线程(它总是让我感到困惑),我是否应该按照上面的代码进行操作?
fetched.set(true);
了。 - Peter LawreyrowCount
有点丑陋,因为您正在违反问题的隐式不变量。使用单独的布尔值指示行已更新会更好吧? - Tudor