java - 创建具有一组线程来处理请求的服务器的指南

标签 java multithreading

假设我有 1000 封流行电子邮件,我必须从中提取电子邮件。

我想创建一个利用多个线程的服务,这样我就可以同时从多个 pop3 服务器提取电子邮件(与序列化进程相反)。

我该如何去做呢?

最佳答案

使用ThreadPoolExecutor .

API 文档中 ThreadPoolExecutor 的描述:

public class ThreadPoolExecutor extends AbstractExecutorService

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

这里很简单tutorial理解相同。

一些更多的链接来理解同样的内容:

http://programmingexamples.wikidot.com/threadpoolexecutor

关于java - 创建具有一组线程来处理请求的服务器的指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3629071/

相关文章:

multithreading - Future::spawn() 有点没用?我怎样才能多线程呢?

java - DB 命中是否比访问 Java 中的集合成本低?

java - 如何使用变量名调用java方法?

java - 使用 Java 向多个线程提供数据

python - 奇怪的线程行为

java - Java中有多个线程访问同一方法时的线程安全性

java - map 中的扩展类(java)

java - 如何在字符串上突出显示

java - 在 Spring Boot 中访问外部配置的好习惯是什么?

java - 当忙于旋转的Java线程绑定(bind)到物理内核时,是否会因为到达代码中的新分支而发生上下文切换?