Java Multi-Threading and concurrency--thread life cycle and thread pool

Source: Internet
Author: User

Thread life cycle:

Thread pool: A technique for pre-creating threads. The thread pool creates a certain number of threads, puts them in an idle queue, and then re-uses the resources before the task arrives. Reduce the frequent creation and destruction of objects.

The top interface of the thread pool in Java is executor, which is a tool for executing threads.

The thread pool interface is executorservice.

Java.util.concurrent Package: A common utility class in concurrent programming

Executor Interface : An object that performs a committed runnable task.

Executorservice Interface: Executor provides methods for managing termination, as well as a way to generate a future for tracking one or more asynchronous task execution states.

Executors class: factory and practical methods of executor, executorservice, etc. defined in this package.

Some static factories are provided inside the executors class to generate some common thread pools.

Newsinglethreadexecutor: creates a single thread pool. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.

Newfixedthreadpool: creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.

Newcachedthreadpool: creates a cacheable thread pool. If the size of the thread pool exceeds the thread required to process the task, then a partially idle (60 second non-performing task) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.

Newscheduledthreadpool: Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.

 Packagecom.vince;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classExecutordemo { Public Static voidMain (string[] args) {//Create a single-threaded actuatorExecutorservice es=Executors.newsinglethreadexecutor (); //Create a fixed number of thread actuatorsExecutorservice Es1=executors.newfixedthreadpool (3); //Create an executor of a cacheable thread (60 seconds of idle will be recycled)Executorservice es2=Executors.newcachedthreadpool (); //Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasksExecutorservice Es3=executors.newscheduledthreadpool (3); Es2.execute (NewDownloadthread ()); Es2.execute (NewDownloadthread ()); Es2.execute (NewDownloadthread ()); Es2.execute (NewDownloadthread ()); }        Static classDownloadthreadImplementsrunnable{@Override Public voidrun () { for(inti=0;i<10;i++) {System.out.println (Thread.CurrentThread (). GetName ()+ "downloaded" +i*10+ "%"); Try{Thread.Sleep (500); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); }            }                    }            }}

Java Multi-Threading and concurrency--thread life cycle and thread pool

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.