这是我的 onCreate 的一部分,有时会导致异常:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tilisting);
_context = getApplicationContext();
SDName = Environment.getExternalStorageDirectory();
//listview = (ListView)findViewById(R.id.TIlistview);
String TIdir = new File(SDName, "/TitaniumBackup/").toString();
final ArrayList<String> apps = new ArrayList<String>();
final StringBuffer done = new StringBuffer();
Command command = new Command(0,"ls -a "+TIdir+"/*.properties") {
@Override
public void output(int arg0, String arg1) {
synchronized(apps) {
apps.add(arg1);
if (!done.toString().equals("")) {
done.append("done");//oh no
}
}
}
};
try {
RootTools.getShell(true).add(command).waitForFinish();
String attrLine = "";
int ind;
backups = new ArrayList<TIBackup>();
synchronized(apps) {
for (String app : apps) {
try {
TIBackup bkup = new TIBackup(app);
FileInputStream fstream = new FileInputStream(app);
BufferedReader atts = new BufferedReader(new InputStreamReader(fstream));
while ((attrLine = atts.readLine()) != null) {
ind = attrLine.indexOf('=');
if (ind !=-1 && !attrLine.substring(0,1).equals("#"))
bkup.prop.put(attrLine.substring(0,ind), attrLine.substring(ind+1));
}
backups.add(bkup);
atts.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
done.append("done");
}
setListAdapter( new StableArrayAdapter(this,backups));
} catch (InterruptedException e) {
//TODO:errors
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
for (String app : apps) {
导致了异常,尽管之前有 waitforfinish()。
这个更新的代码应该修复它,从输出添加数据,并等待主代码中同步的任何落后者,但是如果您在上面的//oh no 行上设置断点,它仍然会到达这个UI 主代码运行后尝试添加项目的点。那么 waitforfinish() 不是在等待吗?如何防止这种竞争状况?
我也尝试了下面的RootTask
代码,但它似乎停在最后一个readline?
RootTask getProfile = new RootTask() {
@Override
public void onPostExecute(ArrayList<String> result) {
super.onPostExecute(result);
for (String r : result) {
System.out.println(r);
}
}
};
getProfile.execute("ls /data/data/org.mozilla.firefox/files/mozilla/" );
onPostExecute 永远不会运行。
最佳答案
这部分是由 RootTools 中的设计缺陷造成的。我认为问题的关键在于您在 shell 上执行的操作花费的时间比为 shell 命令设置的默认超时时间长。当超时发生时,它只是返回已完成的命令,这就是设计缺陷所在。
我提供了一个新的 jar 来使用以及一些关于此的更多信息。我还弃用了 waitForFinish(),因为我同意它过去是、现在仍然是一个糟糕的解决方案。
https://code.google.com/p/roottools/issues/detail?id=35
如果您有任何疑问或问题,请告诉我:)
关于java - 并发修改异常,尽管等待完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16229891/