Java Asynctask Analysis Internal implementation

Source: Internet
Author: User

sdk3.0, using the internal thread pool, multithreading concurrently executes. Thread pool size equals 5, up to 128

After sdk3.0, a thread is executed using the default serial thread pool, and the next thread is executed sequentially. sdk4.3 when the thread pool size equals 5, up to 128

sdk4.4 thread pool size equals CPU Count + 1, max CPU count * 2 + 1

There are two implementations of the thread pool after sdk3.0, and the default is the Serial thread pool

[Java]View Plaincopyprint?
  1. Public static final Executor serial_executor = new Serialexecutor ();
  2. Public static final Executor thread_pool_executor
  3. = New Threadpoolexecutor (Core_pool_size, Maximum_pool_size, Keep_alive,
  4. Timeunit.seconds, Spoolworkqueue, sthreadfactory);
  5. Private static volatile Executor sdefaultexecutor = Serial_executor;
  6. Public static void Setdefaultexecutor (Executor exec) {//Set default thread pool
  7. Sdefaultexecutor = exec;
  8. }
Serialexecutor, using a synchronous lock, execute one thread at a time [Java]View Plaincopyprint?
  1. Private static class Serialexecutor implements Executor {
  2. final arraydeque<runnable> mtasks = new arraydeque<runnable> ();
  3. Runnable mactive;
  4. public synchronized Void execute (final Runnable R) {
  5. Mtasks.offer (new Runnable () {
  6. public Void Run () {
  7. try {
  8. R.run ();
  9. } finally {
  10. Schedulenext ();
  11. }
  12. }
  13. });
  14. if (mactive = = null) {
  15. Schedulenext ();
  16. }
  17. }
  18. protected synchronized void Schedulenext () {
  19. if ((mactive = Mtasks.poll ()) = null) {
  20. Thread_pool_executor.execute (mactive);
  21. }
  22. }
  23. }
  24. sdk3.0, using the internal thread pool, multithreading concurrently executes. Thread pool size equals 5, up to 128

    After sdk3.0, a thread is executed using the default serial thread pool, and the next thread is executed sequentially. sdk4.3 when the thread pool size equals 5, up to 128

    sdk4.4 thread pool size equals CPU Count + 1, max CPU count * 2 + 1

    There are two implementations of the thread pool after sdk3.0, and the default is the Serial thread pool

    [Java]View Plaincopyprint?
    1. Public static final Executor serial_executor = new Serialexecutor ();
    2. Public static final Executor thread_pool_executor
    3. = New Threadpoolexecutor (Core_pool_size, Maximum_pool_size, Keep_alive,
    4. Timeunit.seconds, Spoolworkqueue, sthreadfactory);
    5. Private static volatile Executor sdefaultexecutor = Serial_executor;
    6. Public static void Setdefaultexecutor (Executor exec) {//Set default thread pool
    7. Sdefaultexecutor = exec;
    8. }
    Serialexecutor, using a synchronous lock, execute one thread at a time[Java]View Plaincopyprint?
    1. Private static class Serialexecutor implements Executor {
    2. final arraydeque<runnable> mtasks = new arraydeque<runnable> ();
    3. Runnable mactive;
    4. public synchronized Void execute (final Runnable R) {
    5. Mtasks.offer (new Runnable () {
    6. public Void Run () {
    7. try {
    8. R.run ();
    9. } finally {
    10. Schedulenext ();
    11. }
    12. }
    13. });
    14. if (mactive = = null) {
    15. Schedulenext ();
    16. }
    17. }
    18. protected synchronized void Schedulenext () {
    19. if ((mactive = Mtasks.poll ()) = null) {
    20. Thread_pool_executor.execute (mactive);
    21. }
    22. }
    Thread_pool_executor Concurrent Thread pool

    Asynctask.setdefaultexecutor (Asynctask.thread_pool_executor); Set up to use the concurrency thread pool

    Asynctask.executeonexecutor (executor); You can customize the thread pool using both of these methods


Thread_pool_executor Concurrent Thread pool

Asynctask.setdefaultexecutor (Asynctask.thread_pool_executor); Set up to use the concurrency thread pool

Asynctask.executeonexecutor (executor); You can customize the thread pool using both of these methods

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.