JDK5 new features thread pool (1), jdk5 new features Thread Pool

Source: Internet
Author: User

JDK5 new features thread pool (1), jdk5 new features Thread Pool

1. Concept of thread pool:

In fact, we have been familiar with many concepts about the pool so far: string pool and connection pool. The purpose of this pool is only one: Reuse of resources.

Thread Pool: first, create some threads. After the server receives a customer request, it extracts an idle thread from the thread pool for the service. After the service is complete, the thread is not closed, instead, return the thread back to the thread pool.

In the programming mode of the thread pool, the task is submitted to the entire thread pool, rather than directly to a thread. After the thread pool obtains the task, it finds any Idle threads internally, find the task and then hand it over to an internal idle thread. This is encapsulation. Remember: a task is submitted to the entire thread pool. One thread can only execute one task at the same time, but multiple tasks can be submitted to one thread pool at the same time.


Ii. Create and close a thread pool:

Three different thread pools can be created:

1. Create a fixed thread pool

ExecutorService threadPool = Executors. newFixedThreadPool (3); // 3 threads

2. Create a cache Thread Pool

ExecutorService threadPool = Executors. newCachedThreadPool ();

3. Create a single thread pool

ExecutorService threadPool = Executors. newSingleThreadExecutor ();

public class ThreadPoolTest {public static void main(String[] args) {ExecutorService threadPool = Executors.newSingleThreadExecutor();for (int i = 1; i <= 10; i++) {final int task = i;threadPool.execute(new Runnable() {@Overridepublic void run() {for (int j = 1; j <= 10; j++) {try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);}}});}System.out.println("all of 10 takes have committed!!");threadPool.shutdown();  }}

Close the thread pool:

ThreadPool. shutdown ()

ThreadPool. shutdownNow ()


3. Start the timer with the thread pool:

You can specify the time interval for repeated tasks. Absolute Time is not supported. You must convert the time interval to relative time:

Public class TimerTest {public static void main (String [] args) {// bomb Executors that only explodes once. newScheduledThreadPool (3 ). schedule (new Runnable () {@ Overridepublic void run () {System. out. println ("bombing !!! ") ;}}, 4, TimeUnit. SECONDS); // a serial bomb that will trigger multiple times. // The Executors will be blown up every four SECONDS, and the Executors will be blown up every two SECONDS. newScheduledThreadPool (3 ). scheduleAtFixedRate (new Runnable () {@ Overridepublic void run () {System. out. println ("bombings !!! ") ;}}, 4, 2, TimeUnit. SECONDS );}}




Different JDK versions of JAVA

Conflicts between Versions later than 1.5 may be small, and 1.4 and 1.5 are very prone to conflicts. Many new features have been added since 1.5, such as the annotation generic type.

C # Is it better to use a thread or a thread pool?

If your client uploads data, there is little difference between using a thread or thread pool. The advantage of the thread pool is that the maximum number of parallel threads can be controlled, which plays a significant role in Server programming.
With the increase in the number of requests, if a new thread is opened for each request, the thread will increase sharply, occupying a large amount of instantaneous memory overhead. The thread is a highly open data structure, each thread requires about 1 MB of memory. After the threads in the thread pool are used up, they can be immediately closed. If all the threads in the pool are occupied by tasks, the clr will not create a new thread, instead, wait for other threads in the pool to return to the available status.
If the interface is only prevented from being suspended, asynchronous operations are sufficient, although the thread pool is still used for asynchronous operations.

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.