"Thread Pool"

Source: Internet
Author: User

Program startup a new thread cost is relatively high because it involves interacting with the operating system. Using a thread pool can improve performance, especially when you want to create a lot of short-lived threads in your program.

Line constructor Each thread code ends and does not die, but returns to the thread pool again to become idle, waiting for the next object to be used.

Before JDK5, we had to implement our own thread pool manually, starting with JDK5, the Java built-in support thread pool JDK5 A new executors factory class to generate the thread pool, there are several ways

public static Executorservice Newcachedthreadpool ()

public static Executorservice newfixedthreadpool (int nthreads)

public static Executorservice Newsinglethreadexecutor ()

The return value of these methods is the Executorservice object, which represents a thread pool that can execute runnable objects or the threads represented by callable objects. It provides the following methods

Future<?> Submit (Runnable Task)
<T> future<t> Submit (callable<t> Task)

Case Demo
To create a thread pool object
Creating an Runnable instance
The benefit of committing the runnable instance thread pool is that after each thread code of the line constructor ends, it does not die, but instead returns to the thread pool again to become idle, waiting for the next object to be used.

How do you implement thread code?
A: Create a thread pool object that controls the creation of several thread objects.
public static Executorservice newfixedthreadpool (int nthreads)

B: Threads of this thread pool can execute:
Can execute Runnable object or thread represented by callable object
Do a class to implement the Runnable interface.
C: Call the following method to
Future<?> Submit (Runnable Task)
<T> future<t> Submit (callable<t> Task)
D: I'm going to end the thread pool, okay?
OK. Shutdown ()

 Package com.test;  Public class Implements Runnable {    @Override    publicvoid  run () {        for(  int x=0;x<10;x++) {            System.out.println (Thread.CurrentThread (). GetName ()+ ":" +x);        }    }}
 Packagecom.test;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors; Public classExecutorstest { Public Static voidMain (string[] args) {//creates a thread pool object that controls how many thread objects to create. Executorservice pool = Executors.newfixedthreadpool (2); //can execute Runnable object or thread represented by callable objectPool.submit (Newmyrunnable ()); Pool.submit (Newmyrunnable ()); //End thread PoolPool.shutdown (); }}

Operation Result:

pool-1-thread-2:0Pool-1-thread-2:1Pool-1-thread-2:2Pool-1-thread-2:3Pool-1-thread-2:4Pool-1-thread-2:5Pool-1-thread-2:6Pool-1-thread-2:7Pool-1-thread-2:8Pool-1-thread-2:9Pool-1-thread-1:0Pool-1-thread-1:1Pool-1-thread-1:2Pool-1-thread-1:3Pool-1-thread-1:4Pool-1-thread-1:5Pool-1-thread-1:6Pool-1-thread-1:7Pool-1-thread-1:8Pool-1-thread-1:9

"Thread Pool"

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.