Simple use of thread pool, simple use of Thread Pool
Create a thread pool with a specified number of threads
private static ExecutorService scheduledTaskFactoryExecutor = null;
private boolean isCancled = false;
private static class ThreadFactoryTest implements ThreadFactory {
@ Override
Public Thread newThread (Runnable r ){
Thread thread = new Thread (r );
Thread. setName (TAG );
// Set Thread to user Thread through Thread. setDaemon (false); Set daemon to daemthread through Thread. setDaemon (true)
// After the main thread ends, the user thread will continue to run and the JVM will survive. After the main thread ends, the status of the daemon thread and JVM will be determined by the following 2nd items:
// If no user thread exists, all are daemon threads, then the JVM ends (and all the daemon threads are removed)
// After the android program exits, the jvm will be recycled, while after the java program exits based on services, the jvm will not immediately Recycle
Thread. setDaemon (true );
Return thread;
}
}
static {
scheduledTaskFactoryExecutor = Executors.newFixedThreadPool(3, new ThreadFactoryTest());
scheduledTaskFactoryExecutor.submit(new Runnable() {
@Override
public void run() {
Log.i(TAG, "This is the ThreadFactory Test submit Run! ! ! ");
}
});
}
AsyncTaskTest task = new AsyncTaskTest(url);
// Execute the defined thread in the thread pool
task.executeOnExecutor(scheduledTaskFactoryExecutor);
mTaskList.add(task);
Class AsyncTaskTest extends AsyncTask <Void, Integer, Void> {// asynchronous task
Private String url;
Public AsyncTaskTest (String url ){
This. url = url;
}
@ Override
Protected Void doInBackground (Void... params ){
If (! IsCancelled () & isCancled = false) // this location is critical. If no flag is set, setCancel (true) is invalid.
{
Bitmap bmp;
If (! Url. contains ("http") {// network image
Bmp = ImageLoader. getInstance (). loadImageSync ("file: //" + url, options );
} Else {
Bmp = ImageLoader. getInstance (). loadImageSync (url, options );
}
If (bmp! = Null ){
Byte [] B = new byte [0];
Try {
B = MyImageUtil. getImageThumbnailBase (bmp );
Upload (B );
PublishProgress (count); // update progress bar
} Catch (IOException e ){
UMDocApplication. getInstance (). getLog (). e (e );
}
Log. d (TAG, "byte length" + B. length );
}
}
Return null;
}
@ Override
Protected void onProgressUpdate (Integer... values ){
Log. d (TAG, "count" + values [0]);
If (values [0] = 1)
ProBar. setVal (count );
}
}