"Go" Java Concurrency: Executors and thread pool

Source: Internet
Author: User
Tags vcard

Original address: http://baptiste-wicht.com/posts/2010/09/java-concurrency-part-7-executors-and-thread-pools.html

Java Concurrency-part 7:executors and thread pools

Baptiste Wicht

2010-09-15 07:17

Comments

Source

Let's start with a new post in the Java concurrency series.

This time we'll learn how to start cleanly new threads and to manage thread pools. In Java, if you had a Runnable like this:

RunnablerunSystem.  Out.  println("Run"}}         

You can easily run it in a new thread:

Thread(runnable).  Start();    

This was very simple and clean, but what if you ' ve several long running the tasks that you want to the load in Parralel and then WA It for the completion of all the tasks, it's a little bit harder to code and if you want to get the return value of all th e tasks it becomes really difficult to keep a good code. But as for almost any problems, Java have a solution for you, the executors. This simple class allows your to create thread pools and thread factories.

A thread pool is represented by an instance of the class Executorservice. With a executorservice, you can submit a task that would be a completed in the future. Here is the type of thread pools you can create with the executors class:

  • Single thread Executor : A thread pool with only one thread. So all the submitted task would be executed sequentially. Method:executors.newsinglethreadexecutor ()
  • Cached thread pool : A thread pool that is create as many threads it needs to execute the task in Parralel. The old available threads is reused for the new tasks. If a thread is not used during seconds, it'll be terminated and removed from the pool. Method: executors.newcachedthreadpool ()
  • fixed thread pool : A thread pool with a fixed number of threads. If A thread is a does available for the task, the task was put in the queue waiting for a and task to ends. Method: executors.newfixedthreadpool ()
  • scheduled thread pool : A thread pool made to schedule the future task. Method: executors.newscheduledthreadpool ()
  • Single thread scheduled pool : A thread pool with only one thread to schedule the future task. Method:executors.newsinglethreadscheduledexecutor ()

Once you has a thread pool, you can submit task to it using the different submit methods. You can submit a Runnable or a callableto the thread pool. The method return a future representing the "the". If you submitted a Runnable, the future object return null once the task finished.

By example, if you have this callable:

Callable<Stringcall"Run"}}     

If you want to execute the task ten times using 4 threads, you can use the that code:

Executors.  Newfixedthreadpool(4);  For(0ipool.  Submit(stringtask());}             

But you must shutdown the thread pool in order to terminate all the threads of the pool:

Pool.  Shutdown();  

If you don't have the JVM risk to not shutdown because there are still threads not terminated. You can also force the shutdown of the pool using Shutdownnow, with the currently running tasks would be interrupted a nd the tasks not started won't is started at all.

But with this example, you cannot get the result of the task. So let's get the future objects of the tasks:

ExecutorservicePool=Executors.Newfixedthreadpool(4);List<Future<String>>Futures=NewArrayList<Future<String>> (10);For(IntI=0;I<10;I++) {futures. (pool. Submit (new stringtask ())); } for (future<string > future : futures) { string result = future. Get (); //compute the Result}pool. Shutdown ()               

But this code is a bit complicated. And there is a disadvantage. If The first task takes a long time to compute and all the other tasks ends before the first, the current thread cannot co Mpute the result before the first task ends. Once again, Java have the solution for you, Completionservice.

A completionservice is a service this make easier to wait for the result of submitted task to an executor. The implementation is Executorcompletionservice who's based on a executorservice to work. So let ' s try:

ExecutorserviceThreadPool=Executors.Newfixedthreadpool(4);Completionservice<String>Pool=NewExecutorcompletionservice<String> (ThreadPool);For(IntI=0;I<10;I++){pool. (new stringtask ());  for (int i = 0< Span class= "O"; i < 10i++) {string result = pool. ().  (); //compute the Result}threadpool. Shutdown ()               

With this, you had the result in the order they is completed and you had to keep a collection for the future.

Here is the tools in hand to launch tasks in Parralel using performing thread pools. Using executors, Executorservice and completionservice you can create complex algorithm using several taks. With that tools, it's really easy-to-change the number of threads performing in parralel or adding more tasks without Chan Ging a lot of code.

I hope that this post would help you to write better concurrent code.

"Go" Java Concurrency: Executors 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.