Java thread pool

Source: Internet
Author: User

The role of the thread pool:

provide system efficiency; If you create a thread each time a request arrives, the overhead is considerable, and the server creates and destroys threads that spend a considerable amount of time and system resources, and if the number of threads is too large, the switch between threads consumes a lot of time and resources. By reusing threads for multiple tasks, the overhead of thread creation and destruction is shared across multiple tasks, and the average time and resources spent on each task is much less.

1, Newcachedthreadpool

Create a cacheable thread pool that can be flexibly reclaimed if the thread pool size exceeds the size required to process it, and if it cannot be recycled, create threads that could be used to perform new tasks

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

public class Cachedthreadpool {

@Override

public void Run () {

Executorservice Cachedthreadpool = Executors.newcachedthreadpool ();

for (int i = 0; i < i++) {

Final int index = i;

try {

Thread.Sleep (Index * 1000);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

Cachedthreadpool.execute (New Runnable () {

@Override

public void Run () {

SYSTEM.OUT.PRINTLN (index);

}

});

}

}

}

2, Newfixedthreadpool

Creates a thread that controls the maximum number of concurrent threads, and the thread that is out of the queue waits for

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

public class Fixedthreadpool {

@Override

public void Run () {

Executorservice Fixedthreadpool = Executors.newfixedthreadpool (3);

for (int i = 0; i < i++) {

Final int index = i;

Fixedthreadpool.execute (New Runnable () {

@Override

public void Run () {

try {

SYSTEM.OUT.PRINTLN (index);

Thread.Sleep (2000);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

}

});

}

}

}

3, Newscheduledthreadpool

Create a fixed-line thread that supports deferred execution or periodic execution at set time

Import java.util.concurrent.Executors;

Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;

public class Scheduledthreadpool {

@Override

public void Run () {

Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (3);

Scheduledthreadpool.scheduleatfixedrate (New Runnable () {

@Override

public void Run () {

System.out.println ("3 seconds delay");

}

}, 1, 3, timeunit.seconds);

}

}

4, Newsinglethreadpool

Single threaded thread pool, execution order of all tasks in the order specified (FIFO, LIFO, priority, etc.)

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

public class Singlethreadpool {

@Override

public void Run () {

Executorservice Singlethreadpool = Executors.newsinglethreadexecutor ();

for (int i = 0; i < i++) {

Final int index = i;

Singlethreadpool.execute (New Runnable () {

@Override

public void Run () {

try {

SYSTEM.OUT.PRINTLN (index);

Thread.Sleep (2000);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

}

});

}

}

}

Java 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.