Java:在服务器应用程序中创建线程池

发布于 2024-11-06 06:38:54 字数 333 浏览 2 评论 0原文

我想为一系列数据库调用(串行)创建一个线程池。我们希望节省这些毫秒。所以我们不想浪费时间串行执行数据库查询。 我正在开发一个已经有许多并行节点的服务器应用程序。在其中一个节点中有一系列数据库调用。我想在已经与其他节点并行运行的节点内引入并行性。

线程池执行器是一个好的选择吗?我不知道我会运行多少个查询。这取决于请求对象的状态。所以我无法修复线程池的队列大小。

这是我找到的示例。

这有效吗?还有其他选择吗?任何建议将不胜感激。

I want to create a ThreadPool for a series of database calls(serial). We want to save those milliseconds. So we don't want to waste time in executing the database queries in serial.
I'm working on a server application which already have many parallel nodes. In one of those nodes there are a series of database calls. I want to introduce parallelism inside a node that already running in parallel with other nodes.

Is thread pool executor a good choice? I don't know how many queries I'll be running. It depends on the state of the request object. So I can't fix the queue size of the thread pool.

This is the example that I have found.

Is this efficient? Is there any other alternative? Any suggestions will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-11-13 06:38:55

在 Java EE 环境中生成自己的线程通常是一个坏主意。有时必须这样做,但如果有其他选择,就不应该这样做。我不确定你到底想做什么,以及你使用的 Java EE 版本是什么,但如果它是 6,那么也许你可以使用 异步 EJB。

Spawning your own threads in a Java EE environment is usually a bad idea. Sometimes it has to be done, but you shouldn't do it if there's an alternative. I'm not sure exactly what you're trying to do, and what version of Java EE you're on, but if it's 6, then maybe you could use an asynchronous EJB.

飞烟轻若梦 2024-11-13 06:38:55

您的问题的标准解决方案是使用 JMS。每个查询都应该包装到命令中。命令应作为 JMS 消息发送到队列。 MDB(消息驱动bean)应该接收消息并异步执行查询。

这种方法还有另一个优点:如果您使用多个物理服务器,工作将在它们之间分配,因此系统将更加健壮。

The standard solution for your problem is using JMS. Each query should be wrapped into command. Command should be sent as JMS message to queue. MDB (message driven bean) should receive them message and perform query asynchronously.

This approach has yet another advantage: if your are working with several physical servers the work will be distributed among them, so the system will be more robust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文