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