Multithreading 在执行器上执行AsyncTasks不会获取所有数据
我正在使用asyncTask通过internet下载一些文件。这是我写的代码,它是有效的Multithreading 在执行器上执行AsyncTasks不会获取所有数据,multithreading,android-asynctask,Multithreading,Android Asynctask,我正在使用asyncTask通过internet下载一些文件。这是我写的代码,它是有效的 downloadUrl task = new downloadUrl(url1,"jsonData1","/sdcard/appData/LocalJson/jsonData1",context); task.execute(); downloadUrl task1 = new downloadUrl(url2,"jsonData2"
downloadUrl task = new downloadUrl(url1,"jsonData1","/sdcard/appData/LocalJson/jsonData1",context);
task.execute();
downloadUrl task1 = new downloadUrl(url2,"jsonData2","/sdcard/appData/LocalJson/jsonData2",context);
task1.execute();
downloadUrl task2 = new downloadUrl(url3,"jsonData3","/sdcard/appData/LocalJson/jsonData3",context);
task2.execute();
downloadUrl task3 = new downloadUrl(url4,"jsonData4","/sdcard/appData/LocalJson/jsonData4",context);
task3.execute();
现在,考虑到UI线程,任务并行运行,但它们在彼此之间序列化运行,这非常耗时。因此,我尝试在执行器上执行它们,但问题是这样我会丢失一些文件,这意味着当它们运行序列化时,我会下载38个文件,而在执行器上运行时,我会下载20个文件。我很确定这是因为我在多线程代码中弄乱了一些东西,所以我将把它发布到:
@Override
protected String doInBackground(String... args) {
downloadAndStoreJson(url,targetFolder);
JSONObject jsonObj = loadJSONObject(pathForLoad);
try {
processJsonData(jsonObj);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "done";
}
@Override
protected void onPostExecute(String result) {
s(targetFolder+" Finished!");
++mutex;
progressBar.setProgress(25*mutex);
if(mutex==4){
mutex=0;
progressBar.setProgress(100);
progressBar.dismiss();
s(monuments.size());
Intent intent = new Intent (getApplicationContext(),NextClass.class);
intent.putExtra("monuments", monuments);
startActivity(intent);
}
}
private void downloadAndStoreJson(String url,String tag){
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
String jsonString = json.toString();
byte[] jsonArray = jsonString.getBytes();
File fileToSaveJson = new File("/sdcard/appData/LocalJson/",tag);
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(fileToSaveJson));
bos.write(jsonArray);
bos.flush();
bos.close();
} catch (FileNotFoundException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
jsonArray=null;
jParser=null;
System.gc();
}
}
private JSONObject loadJSONObject(String path){
JSONObject jsonObj = null;
File readFromJson = new File(path);
byte[] lala;
try {
lala= org.apache.commons.io.FileUtils.readFileToByteArray(readFromJson);
s("---------------"+lala.length);
String decoded = new String(lala, "UTF-8");
jsonObj = new JSONObject(decoded);
} catch (IOException e5) {
// TODO Auto-generated catch block
e5.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObj;
}
processJsonData是一个很长的方法,它解析json文件,创建对象,然后将它们存储在ArrayList中,这可能是存在问题的地方。您需要确保代码是可重入的,这意味着必须能够由多个线程同时运行它。或者,如果某些代码用于同步线程之间的执行,则需要确保它是同步的 查看代码,我发现互斥量是一个静态变量,用于跟踪线程。确保互斥体上的操作是同步的,只是为了保持其干净。但这不会给你带来麻烦 我在这个代码片段中没有看到您的错误,或者我没有看到问题,或者它可能位于其他一些方法中?你能分享下载和存储JSON吗