Java Multi Thread ~ ~ ~ Use Threadpoolexecutor to create threads

Source: Internet
Author: User

We used to create threads with the active new thread and then call their start method, but if the threads are very numerous, the task is not

Often, this kind of writing will be very troublesome, of course, the efficiency is not very high, Java provides us with the thread creator called the concept of the class,

He can help us manage these threads, and all you have to do is write the code and give it to him and she will help you run it automatically.

Of course, ThreadPool with the cache will have a very good performance on the dead thread, but not too many threads

Give him management, or the system will be dragged down, let's do an example.

Package Com.bird.concursey.charpet6;import Java.util.date;import Java.util.concurrent.timeunit;public class Task Implements Runnable {//Store the creation date of the Taskprivate date initdate;//store the name of the Taskprivate Stri ng Name;public Task (String name) {this.initdate = new Date (); this.name = name;} @Overridepublic void Run () {System.out.printf ("%s:task%s:created on:%s\n", Thread.CurrentThread (). GetName (), name, in Itdate);  System.out.printf ("%s:task%s:started on:%s\n", Thread.CurrentThread (). GetName (), Name, New Date ()); try {Long duration = (long) (Math.random () * 10); System.out.printf ("%s:task%s:doing a Task during%d seconds\n", Thread.CurrentThread (). GetName (), name, duration); TimeUnit.SECONDS.sleep (duration);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.printf ("%s:task%s:finished on:%s\n", Thread.CurrentThread (). GetName (), Name,new Date ());}}

Package Com.bird.concursey.charpet6;import Java.util.concurrent.executors;import java.util.concurrent.threadpoolexecutor;/** * Execute every task it receives using an executor *  * @author Bird 2014 9 Month 23rd PM 9:03:01 */public Class Server {private Threadpoolexecutor executor;public Server () {executor = (threadpoolexecutor ) Executors.newcachedthreadpool ();} public void ExecuteTask (Task Task) {System.out.printf ("server:a New task has arrived\n"); Executor.execute (Task); System.out.printf ("Server:pool Size:%d\n", executor.getpoolsize ()); System.out.printf ("Server:active Count:%d\n", Executor.getactivecount ()); System.out.printf ("server:completed Tasks:%d\n", Executor.getcompletedtaskcount ());} public void Endserver () {Executor.shutdown ();} public static void Main (string[] args) {Server server = new Server (), for (int i = 0; i <; i++) {Task task = new Task ("task" + i); Server.executetask (Task);} Server.endserver ();}}


If a large number of threads enter, he will create an object for each thread, which will overwhelm the system, and executors provides a constructor-side

method, set a maximum number, if the number of threads created exceeds this number, executors will not be able to create this thread, waiting for an already online pool

The threads inside are running out and have free space to continue creating new threads, which makes the system robust.


The code above doesn't need it, we just need to re-instantiate a code where the constructor is:

Public Server () {executor = (threadpoolexecutor) executors.newfixedthreadpool (5);}


Java Multi Thread ~ ~ ~ Use Threadpoolexecutor to create threads

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.