Android thread pool (2) -- ThreadPoolExecutor and its rejection policy RejectedExecutionHandler example

Source: Internet
Author: User

Android thread pool (2) -- ThreadPoolExecutor and its rejection policy RejectedExecutionHandler example
MainActivity is as follows:

Package cc. vv; import java. util. concurrent. linkedBlockingQueue; import java. util. concurrent. threadPoolExecutor; import java. util. concurrent. timeUnit; import android. OS. bundle; import android. app. activity;/*** Demo Description: * ThreadPoolExecutor and its rejection policy (RejectedExecutionHandler) Example ** Working principle: * The thread pool mainly involves corePoolSize, workQueue, maximumPoolSize, RejectedExecutionHandler * Call principle: * 1 when the number of threads in the thread pool is less than corePoolSiz E is used to create a thread and process the request. * 2 when the number of threads in the thread pool is equal to corePoolSize, the request is put into workQueue, the Idle threads in the thread pool fetch tasks from the workQueue and process the tasks. * 3 when the workQueue is full and cannot store new tasks, the new thread enters the pool and processes the requests; * if the number of threads in the thread pool is greater than maximumPoolSize, use RejectedExecutionHandler to apply certain policies for Denial of Service. ** the mechanism also contains keepAliveTime, which is described as follows: * when the number of threads is greater than the core, * this is the maximum time that excess idle threads will wait for new tasks before terminating. * What does it mean? * For example, there are five threads in the thread pool, three of which are core threads and the other two are non-core threads. * When the non-core thread of keepAliveTime is still idle (that is, No task is executed or no task can be executed), the non-core thread will be terminated. * This is the maximum duration for non-core and Idle threads in the thread pool. After this time is exceeded, the thread is terminated. * ** four denial policies of RejectedExecutionHandler ** hreadPoolExecutor. abortPolicy: * If the number in the thread pool is equal to the maximum number of threads, java is thrown. util. concurrent. rejectedExecutionException. * tasks involving this exception will not be executed. ** ThreadPoolExecutor. callerRunsPolicy (): * When the number in the thread pool is equal to the maximum number of threads, the current task is retried. It will automatically call the execute () method again ** ThreadPoolExecutor. discardOldestPolicy (): * when the number of threads in the thread pool is equal to the maximum number of threads, discard the tasks in the Job Queue header in the thread pool (that is, the Oldest task with the longest wait time ), and execute the new input task ** ThreadPoolExecutor. discardPolicy (): * When the number in the thread pool is equal to the maximum number of threads, discard new tasks that cannot be executed. ** reference: * http://blog.csdn.net/cutesource/article/details/6061229 * http://blog.csdn.net/longeremmy/article/details/8231184 * http://blog.163.com/among_1985/blog/static/275005232012618849266/ * http://blog.csdn.net/longeremmy/article/details/8231184 * http://ifeve.com/java-threadpool/ * http://www.blogjava.net/xylz/archive/2010/07/08/325587.html * http://blog.csdn.net/ns_code/article/details/17465497 * Thank you very much */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); testRejectedExecutionHandler () ;}// test the four policies here and replace the last parameter of the ThreadPoolExecutor () method. private void testRejectedExecutionHandler () {int produceTaskMaxNumber = 10; // construct a thread pool ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor (2, 4, 3, TimeUnit. SECONDS, new parameter blockingqueue
 
  
(3), new ThreadPoolExecutor. discardPolicy (); for (int I = 1; I <= produceTaskMaxNumber; I ++) {try {String task = "task" + I; System. out. println ("put" + task + "into the thread pool" into threadpoolexecutor.exe cute (new RunnableImpl (task);} catch (Exception e) {e. printStackTrace (); System. out. println ("AbortPolicy throws an exception ---->" + e. toString () ;}} private class RunnableImpl implements Runnable {private String taskName; private int consumerTaskSleepTime = 2000; RunnableImpl (String taskName) {this. taskName = taskName;} public void run () {System. out. println ("START" + taskName); try {// simulate time-consuming task Thread. sleep (consumerTaskSleepTime);} catch (Exception e) {e. printStackTrace ();} System. out. println ("finished" + taskName );}}}
 

Main. xml is as follows:

     
  
 


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.