Simple use of thread pool, simple use of Thread Pool

Source: Internet
Author: User

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 );
}

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.