Java Executors (thread pool)

Source: Internet
Author: User

Sun has made a lot of extensions to Java Thread class libraries in Java 5. Among them, the thread pool is one of the new features of Java 5. Besides the thread pool, there are also a lot of multithreading-related content, it brings great convenience to multi-threaded programming. To write efficient, stable, and reliable multi-threaded programs, it is particularly important to add content to the thread section.

All content about the new features of Java 5 threads is under java. util. Concurrent, which contains a large number of interfaces and classes. familiarity with these API features is a difficult learning process. At present, there are few materials and books related to this aspect. Books about thread are still on the knowledge level before Java 5.

Of course, the new feature does not have to do with multi-threaded programs. Before Java 5, you can write excellent multi-threaded programs. The price is different.

The basic idea of the thread pool is also the idea of an object pool, which opens up a piece of memory space and stores a large number of threads (not dead). The thread execution scheduling in the pool is handled by the pool manager. When there is a thread task, take one from the pool. After the execution is complete, the thread object is returned to the pool. This avoids the performance overhead caused by repeated creation of thread objects and saves system resources.

Before Java 5, it is quite difficult to implement a thread pool. Now Java 5 has done everything for us, and we only need to use it according to the provided API, you can enjoy the great convenience brought by the thread pool.

Java 5 thread pools are divided into two types: fixed-size thread pools and variable-size connection pools.

Before using the thread pool, you must know how to create a thread pool. in Java 5, you must understand Java. util. concurrent. executors class API. This class provides a large number of static methods for creating connection pools.

1. fixed thread pool, newfixedthreadpool:

Package app.exe cutors; import Java. util. concurrent. executors; import Java. util. concurrent. executorservice;/*** Java thread: thread pool ** @ author Feng Xiaowei */public class test {public static void main (string [] ARGs) {// create a thread pool that can reuse a fixed number of threads executorservice pool = executors. newfixedthreadpool (5); // create thread T1 = new mythread (); thread t2 = new mythread (); thread T3 = new mythread (); thread t4 = new mythread (); thread T5 = ne W mythread (); // specify cute (T5); // close the thread pool. shutdown () ;}} class mythread extends thread {@ overridepublic void run () {system. out. println (thread. currentthread (). getname () + "running... ");}}

Output result:

Pool-1-thread-1 is being executed... Pool-1-thread-3 is being executed... Pool-1-thread-4 is being executed... Pool-1-thread-2 is being executed... Pool-1-thread-5 is being executed...

Change the parameters in executorservice pool = executors. newfixedthreadpool (5): executorservice pool = executors. newfixedthreadpool (2). The output result is:

Pool-1-thread-1 is being executed... Pool-1-thread-1 is being executed... Pool-1-thread-2 is being executed... Pool-1-thread-1 is being executed... Pool-1-thread-2 is being executed...

From the above results, we can see that the newfixedthreadpool parameter specifies the maximum number of threads that can be run. threads that exceed this number will not run after they are added. Second, the threads added to the thread pool are in the managed State, and the running of the threads is not affected by the addition sequence.

 

2. single-task thread pool, newsinglethreadexecutor:

Just change executorservice pool = executors. newfixedthreadpool (2) in the above Code to executorservice pool = executors. newsinglethreadexecutor ();

Output result:

Pool-1-thread-1 is being executed... Pool-1-thread-1 is being executed... Pool-1-thread-1 is being executed... Pool-1-thread-1 is being executed... Pool-1-thread-1 is being executed...

It can be seen that every time the execute method is called, The run method of thread-1 is actually called.

 

3. Variable-size thread pool, newcachedthreadpool:

Similar to the above, we only changed the pool creation method: executorservice pool = executors. newcachedthreadpool ();

Output:

Pool-1-thread-1 is being executed... Pool-1-thread-2 is being executed... Pool-1-thread-4 is being executed... Pool-1-thread-3 is being executed... Pool-1-thread-5 is being executed...

This method has the following features: You can create thread pools for new threads as needed, but they will be reused when previously constructed threads are available.

4. Delayed connection pool, newscheduledthreadpool:

Package app.exe cutors; import Java. util. concurrent. executors; import Java. util. concurrent. scheduledexecutorservice; import Java. util. concurrent. timeunit;/*** Java thread: thread pool ** @ author Feng Xiaowei */public class test {public static void main (string [] ARGs) {// create a thread pool that can be scheduled to run commands or periodically after a given delay. Scheduledexecutorservice pool = executors. newscheduledthreadpool (2); // creates and implements the runnable interface object. Of course, the thread object also implements the runnable interface thread T1 = new mythread (); thread t2 = new mythread (); thread T3 = new mythread (); // put the thread into the pool and execute pool.exe cute (T1); // use the delayed execution method pool. schedule (t2, 1000, timeunit. milliseconds); pool. schedule (T3, 10, timeunit. milliseconds); // closes the thread pool. shutdown () ;}} class mythread extends thread {@ overridepubl IC void run () {system. Out. println (thread. currentthread (). getname () + "is being executed... ");}}

You can try to change the parameters of executors. newscheduledthreadpool (2) to get more experience.

@ Overridepublic void run () {system. Out. println (thread. currentthread (). getname () + "is being executed... ");}

 

To an infinite loop, you can get more usage about pool. Shutdown.

 

5. single-task delay connection pool (similar to above, it will not be written ). Of course, we can also customize the thread pool. We will not write it here. tired ......

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.