You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
960 B
Java
30 lines
960 B
Java
6 months ago
|
package com.xypower.mppreview;
|
||
|
|
||
|
import android.os.AsyncTask;
|
||
|
import java.util.concurrent.Executor;
|
||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
|
||
|
public class AsyncTaskWithCustomThreadPool extends AsyncTask<Void, Void, Void> {
|
||
|
|
||
|
private static final int CORE_POOL_SIZE = 2; // 自定义线程数
|
||
|
private static final int MAXIMUM_POOL_SIZE = 2; // 自定义线程数
|
||
|
private static final int KEEP_ALIVE = 3; // 自定义线程存活时间
|
||
|
|
||
|
public static final Executor THREAD_POOL_EXECUTOR
|
||
|
= new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
|
||
|
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
|
||
|
|
||
|
|
||
|
|
||
|
@Override
|
||
|
protected Void doInBackground(Void... params) {
|
||
|
// 你的后台操作
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public void executeTask() {
|
||
|
executeOnExecutor(THREAD_POOL_EXECUTOR);
|
||
|
}
|
||
|
}
|