在 servlet 中使用线程

发布于 2024-12-28 18:37:40 字数 89 浏览 0 评论 0原文

我很困惑我们是否应该在 servlet 中创建自己的线程,因为它们有线程机制 内部?如果是,我们如何确定程序是否线程安全?如何在servlet中实现线程安全机制。

I am confused if we should make our own threads in servlet or not,as they have threading mechanism
internally?. If yes how can we make sure if the program thread safe? How to implement thread safe mechanism in servlets.

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

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

发布评论

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

评论(3

懒猫 2025-01-04 18:37:40

您在问两个不同的问题:

我很困惑我们是否应该在 servlet 中创建自己的线程,因为
他们内部有线程机制吗?.

通常,您不应在 Java EE 应用程序中启动线程。如果您需要单独的线程,请确保使用应用程序了解的调度程序服务,以便在应用程序关闭时它有机会关闭线程。石英是最常用的。

如果是,我们如何确定程序是否线程安全?如何
在servlet中实现线程安全机制。

Servlet 就像任何其他 Java 类一样。查找有关线程安全的教程或阅读 Java 并发实践

You are asking two different questions:

I am confused if we should make our own threads in servlet or not,as
they have threading mechanism internally?.

Normally, you should not start threads in a Java EE application. If you need seperate threads, make sure you use a Scheduler Service that your application knows about, so that it has the chance to shut down the threads when the application is shut down. Quartz is what's used most of the time.

If yes how can we make sure if the program thread safe? How to
implement thread safe mechanism in servlets.

Servlets are just like any other Java class. Find a tutorial on thread safety or read Java Concurrency in Practice.

森林迷了鹿 2025-01-04 18:37:40

根据您在评论中所写的内容,我了解到您有一组线程持续监视日志文件并在日志中发现有趣的内容时发送电子邮件。

第一个问题:为什么这是一个servlet?有网络图形用户界面吗?这是做什么用的?

对于日志扫描部分,我会将其实现为 servlet 容器之外的单独进程。对于该进程发现的需要发送到某处的所有内容,我会向 JMS 队列添加一条消息。然后我将创建一个消息驱动 bean 来接收来自该队列的消息并将它们作为电子邮件发送。 (这实际上是一个集成问题,将消息从 JMS 转换为电子邮件,您可能需要研究像 Mule 这样的东西来解决这个问题)。

至于如何将其与您的servlet集成,这取决于您的servlet除了扫描日志之外还做了什么(我想它向用户提供了某种界面)

通过这种设计,您可以选择重新编写生成登录未来。第一个程序最好将感兴趣的消息直接放在 JMS 队列上,而不是让一个程序写入日志并由另一个程序解析日志。换句话说,您将来可以更改架构的日志生成部分,而无需重新编写邮件发送部分。

From what you write in the comment, I understand that you have a set of threads continuusly monitoring log-files and sending email if something interresting is found in the log.

First question: why is this a servlet? Is there a web-gui? What is this used for?

For the log-scanning part, I would have implemented that as a separate process outside of the servlet-container. For everything this process found which it needs to send somewhere, I would add a message to a JMS-queue. Then I would create a messagedriven bean to recieve messages from this queue and send them as email. (This is really an integration problem, transforming messages from JMS to email, you might want to look into something like Mule to solve this).

As for how to integrate this with your servlet, it depends on what your servlet does in addition to scanning logs (I suppose it presents the user with some kind of interface)

With this design, you can chose to re-write the programs generating the log in the future. Instead of having one program writing log and another program parsing the log, the first program might as well put the interresting message directly on the JMS-queue. In other words, you can change the log-generation part of your architecture in the future, without having to re-write the mail-sending part.

漫雪独思 2025-01-04 18:37:40

我也有类似的担忧。

只有EJB 规范不允许从应用程序创建线程。

从 servlet 启动线程是可以的。
我已经这样做了很多次,没有任何问题,但说实话,我不能 100% 确定:

  • 这是容器允许的,但是违反了标准

,或者

  • 是所有容器都允许的。

但在 Tomcat 中,我从来没有遇到过从 servlet 启动线程的问题。

您可以像在每个多线程程序中一样使其线程安全。

您将使用 Java 提供的所有可用构造来进行同步。

I also had a similar concern.

Only EJB specification disallows the creation of threads from the application.

It is ok to start a thread from a servlet.
I have done it many times with no problems but to be honest I am not 100% sure:

  • that this is allowed by container but is violating a standard

or

  • it is allowed by all containers.

But in Tomcat I never had an issue starting threads from a servlet.

You can make it thread safe the same way you do in every multithreading program.

You will use all the available constructs offered by Java for synchronization.

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