Recommended thread usage by Ali Threadpoolexecutor

Source: Internet
Author: User

Ali recommended reason: Using the thread pool can reduce the time spent on creating and destroying threads and the overhead of system resources, and then the reason for not executors a custom thread pool is to use Threadpoolexecutor to standardize the usage of the thread pool, and to make it easier for others to understand the thread pool's running rules.

Let's talk about the concept of threading.

Task: The code that the thread needs to execute, namely runnable
Task queue: The thread is full, the task is placed in the task queue, wait, and other tasks in the thread after the execution, the threads are empty, the task queue will be the first non-executed task into the thread execution.
Core threads: Threads that thread pool has been in existence
Maximum number of threads: refers to the maximum number of threads that the thread pool can hold

Threadpoolexecutor (int corepoolsize,
int Maximumpoolsize,
Long KeepAliveTime,
Timeunit Unit,
Blockingqueue<runnable> workQueue)

The first is the number of core threads, the second is the maximum number of threads, and the third is the idle timeout for non-core threads, which is recycled over this time, and the fourth is the task queue pattern in the thread pool.

We mainly talk about this task queue pattern

1.LinkedBlockingDeque

I'll start with an example code, and then chit chat when.

public class Mainactivity extends Appcompatactivity {

Button button;ThreadPoolExecutor executor;Runnable myRunnable = new Runnable() {    @Override    public void run() {        try {            Thread.sleep(2000);            System.out.println(Thread.currentThread().getName() + " run");        } catch (InterruptedException e) {            e.printStackTrace();        }    }};@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    button = (Button) findViewById(R.id.btn_record_video);    button.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            executor.execute(myRunnable);            executor.execute(myRunnable);            executor.execute(myRunnable);        }    });    executor = new ThreadPoolExecutor(3, 6, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());}

}

When we click the button once

03-03 15:00:43.503 5292-5333/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:00:43.505 5292-5335/com.example.zth.seven i/system.out:pool-1-thread-3 Run
03-03 15:00:43.505 5292-5334/com.example.zth.seven i/system.out:pool-1-thread-2 Run

Click two consecutive times

03-03 15:10:47.941 7114-7259/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:10:47.941 7114-7260/com.example.zth.seven i/system.out:pool-1-thread-2 Run
03-03 15:10:47.942 7114-7261/com.example.zth.seven i/system.out:pool-1-thread-3 Run
03-03 15:10:49.942 7114-7260/com.example.zth.seven i/system.out:pool-1-thread-2 Run
03-03 15:10:49.942 7114-7259/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:10:49.942 7114-7261/com.example.zth.seven i/system.out:pool-1-thread-3 Run

This time you can see that although the maximum number of threads is 6, but only with the core thread, do not create a new thread, if the core thread ran out of the queue to wait, the size of the queue is not limited (you can casually, he will be executed later anyway)

But it's not over yet. Linkedblockingdeque can also set the number of queues if I set the queue to 2

    executor = new ThreadPoolExecutor(3, 6, 3,            TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(2));

Then click 22 consecutive times

03-03 15:19:39.851 8971-9013/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:19:39.851 8971-9014/com.example.zth.seven i/system.out:pool-1-thread-2 Run
03-03 15:19:39.851 8971-9015/com.example.zth.seven i/system.out:pool-1-thread-3 Run
03-03 15:19:40.045 8971-9016/com.example.zth.seven i/system.out:pool-1-thread-4 Run
03-03 15:19:41.851 8971-9013/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:19:41.851 8971-9015/com.example.zth.seven i/system.out:pool-1-thread-3 Run

He was able to create four threads at this time, why, first click on the three core thread, then the second click to put the first two tasks into the queue, and then the queue is full to create a new thread to perform the last task

If we click three times in a row, the program crashes the third time we click, because the number of threads exceeds the maximum number of threads

Summary: Linkedblockingdeque first uses the core thread, the core thread ran out of the task into the queue to wait, if the queue is full to create a new thread, pay attention to not set the number of queues, he is infinitely large by default.

2.SynchronousQueue

We'll just replace one line of code with the same old.

    executor = new ThreadPoolExecutor(3, 6, 3,            TimeUnit.SECONDS, new SynchronousQueue<Runnable>());

Click once

03-03 15:39:31.915 12403-12446/com.example.zth.seven i/system.out:pool-1-thread-2 Run
03-03 15:39:31.915 12403-12447/com.example.zth.seven i/system.out:pool-1-thread-3 Run
03-03 15:39:31.915 12403-12445/com.example.zth.seven i/system.out:pool-1-thread-1 Run

Click two consecutive times

03-03 15:39:53.204 12403-12446/com.example.zth.seven i/system.out:pool-1-thread-2 Run
03-03 15:39:53.204 12403-12445/com.example.zth.seven i/system.out:pool-1-thread-1 Run
03-03 15:39:53.204 12403-12447/com.example.zth.seven i/system.out:pool-1-thread-3 Run
03-03 15:39:53.371 12403-12501/com.example.zth.seven i/system.out:pool-1-thread-5 Run
03-03 15:39:53.371 12403-12502/com.example.zth.seven i/system.out:pool-1-thread-6 Run
03-03 15:39:53.371 12403-12500/com.example.zth.seven i/system.out:pool-1-thread-4 Run

As you can see, when the core thread fills up, he does not put the task in the queue to wait, but instead creates a new thread to perform the task directly.

Click three consecutive times

The program crashes because the thread he created exceeds the maximum number of threads,

Summary: Synchronousqueue is very simple, do not use the queue, the core thread ran out to create a new thread,

Reference article:

http://blog.csdn.net/qq_25806863/article/details/71126867

Recommended thread usage by Ali Threadpoolexecutor

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.