Java high-tech-thread pool usage

Source: Internet
Author: User

I. Problem INTRODUCTION

We know we can use 1. Inherit the thread Class 2. Implementing the Runnable interface two ways to create a thread, which is convenient to implement, but also has a problem:

If the number of concurrent threads is large, and each thread is executing a short task, then the frequent creation of threads can greatly reduce the efficiency of the system.

Because it takes time to create threads frequently and destroy threads.

so is there a way to enable a thread to be reused, to perform a task, not to be destroyed, but to continue to perform other tasks?

This can be achieved in Java through a thread pool !

Two. Introduction to the thread pool

①jdk1.5 later joined the Java.util.concurrent package, which focuses on Java threads and the use of thread pools. Provides a great deal of help with the problem of threading in development.

How the ② thread pool works:

1. The thread pool is a technique of pre-creating threads that are pre-created with a certain number of threads and placed in an idle queue when no tasks arrive.

These threads are asleep and do not consume the CPU, so they are only taking up less memory

2. When the request arrives, the thread pool allocates an idle thread to the request and processes the request into the thread

3. When the thread pool pre-sets the number of threads that do not meet the requirements too much or too little, you can manually adjust

Benefits of the ③ thread pool

1. Reduces the number of times a thread is created and destroyed, and each worker thread can be reused to perform multiple tasks.

2. You can adjust the number of threads in the thread pool based on the endurance of the system, and prevent the server from getting down because it consumes too much memory (each thread needs about

1MB of memory, the more threads open, The more memory is consumed, the last crashes ).

Three. Getting Started with the thread pool

The top interface of the thread pool in Java is Executor, but strictly speaking, Executor is not a thread pool, but a tool for executing threads.

and the real thread pool interface is Executorservice.

The four most important classes of the thread pool are as follows:

Executorservice

A true thread pool interface.

Scheduledexecutorservice

can be similar to Timer/timertask to solve problems that require repetitive execution of tasks.

Threadpoolexecutor

The default implementation of Executorservice.

Scheduledthreadpoolexecutor

Inheriting Threadpoolexecutor's Scheduledexecutorservice interface implementation, the class implementation of periodic task scheduling.

Java high-tech-thread pool usage

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.