请各位大侠帮忙,这很重要。
我需要在不到 5 分钟的时间内下载大约 2000 张图像。所以我决定进行并行图像下载。
如果有人可以建议我进行并行下载的更好方法或告诉我哪里出错了,我将不胜感激。
在错误日志中,已完成任务的数量总是不同的......
请帮忙。
我正在使用 Artem Zinnatullin 的异步任务执行器
public class AsyncTaskExecutor {
private static final int CORE_POOL_SIZE;
private static final int MAXIMUM_POOL_SIZE;
private static final int KEEP_ALIVE;
private static final TimeUnit TIME_UNIT;
private static final BlockingQueue<Runnable> concurrentPoolWorkQueue;
private static final ThreadFactory concurrentThreadFactory;
private static final ThreadPoolExecutor concurrentExecutor;
private AsyncTaskExecutor() {}
static {
CORE_POOL_SIZE = 5;
MAXIMUM_POOL_SIZE = 128;
KEEP_ALIVE = 1;
TIME_UNIT = TimeUnit.SECONDS;
concurrentPoolWorkQueue = new LinkedBlockingQueue<Runnable>(10);
concurrentThreadFactory = new AsyncTaskThreadFactory();
concurrentExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE, KEEP_ALIVE, TIME_UNIT, concurrentPoolWorkQueue, concurrentThreadFactory);
}
/**
* Concurrently executes AsyncTask on any Android version
* @param task to execute
* @param params for task
* @return executing AsyncTask
*/
@SuppressLint("NewApi")
public static <Params, Progress, Result> AsyncTask<Params, Progress, Result>
executeConcurrently(AsyncTask<Params, Progress, Result> task,
Params... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(concurrentExecutor, params);
} else {
task.execute(params);
}
return task;
}
/**
* Thread factory for AsyncTaskExecutor
* @author Artem Zinnatullin
*
*/
private static class AsyncTaskThreadFactory implements ThreadFactory {
private final AtomicInteger count;
{
count = new AtomicInteger(1);
}
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + count.getAndIncrement());
}
}
}
然后我使用循环来下载我所有的图像。
for (PhotoToDBHelper photoToDBHelper : notDownloaded) {
try {
photoToDBHelper.download(m_context);
} catch (Exception e) {
// TODO: handle exception
}
}
下载方法如下所示:
public void download(Context context) throws Exception {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(new URL(getPhotosUrl() + m_fileName).toString());
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
filePath = Environment.getExternalStorageDirectory().toString() + getFoldeRelativePath() + m_fileName;
Utils.createFileOnExternalStorage(getFoldeRelativePath(), filePath);
FileOutputStream fos = new FileOutputStream(filePath, false);
byte[] buffer = new byte[20000];
int byteRead = 0;
while ((byteRead = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, byteRead);
}
fos.close();
}
当启动我的android应用程序时,我收到了这个错误
02-25 01:08:30.133: E/AndroidRuntime(32357): FATAL EXCEPTION: main
02-25 01:08:30.133: E/AndroidRuntime(32357): java.lang.RuntimeException: Unable to start activity ComponentInfo{"package".TabHostActivity}: java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@2c0809b0 rejected from java.util.concurrent.ThreadPoolExecutor@2be18f48[Running, pool size = 128, active threads = 1, queued tasks = 10, completed tasks = 27]
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.os.Looper.loop(Looper.java:137)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread.main(ActivityThread.java:4441)
02-25 01:08:30.133: E/AndroidRuntime(32357): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 01:08:30.133: E/AndroidRuntime(32357): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 01:08:30.133: E/AndroidRuntime(32357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-25 01:08:30.133: E/AndroidRuntime(32357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-25 01:08:30.133: E/AndroidRuntime(32357): at dalvik.system.NativeStart.main(Native Method)
02-25 01:08:30.133: E/AndroidRuntime(32357): Caused by: java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@2c0809b0 rejected from java.util.concurrent.ThreadPoolExecutor@2be18f48[Running, pool size = 128, active threads = 1, queued tasks = 10, completed tasks = 1032]
02-25 01:08:30.133: E/AndroidRuntime(32357): at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1967)
02-25 01:08:30.133: E/AndroidRuntime(32357): at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:782)
02-25 01:08:30.133: E/AndroidRuntime(32357): at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1303)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:564)
02-25 01:08:30.133: E/AndroidRuntime(32357): at "package".data.AsyncTaskExecutor.executeConcurrently(AsyncTaskExecutor.java:58)
02-25 01:08:30.133: E/AndroidRuntime(32357): at "package".TabHostActivity.checkUpdateState(TabHostActivity.java:130)
02-25 01:08:30.133: E/AndroidRuntime(32357): at "package".TabHostActivity.onCreate(TabHostActivity.java:88)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.Activity.performCreate(Activity.java:4465)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-25 01:08:30.133: E/AndroidRuntime(32357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-25 01:08:30.133: E/AndroidRuntime(32357): ... 11 more