如何从Java EE中启动多个线程?

时间:2022-01-31 22:49:37

I need to scale calls into Tomcat and it's been suggested to launch threads internally. Has anyone needed to do this and, if so, what solutions did they come up with?

我需要将调用扩展到Tomcat,并建议在内部启动线程。有没有人需要这样做,如果有的话,他们提出了什么解决方案?

6 个解决方案

#1


5  

Creating your own threads inside an application server is generally discouraged because the server should manage threads for better scalability. You can also run into problems if the container makes assumptions about what's available in a thread context, such as security information (e.g., authenticated Subject). That typically happens if you spawn a thread and then use a server resource from that thread which is unknown to the container.

通常不鼓励在应用程序服务器中创建自己的线程,因为服务器应该管理线程以获得更好的可伸缩性。如果容器对线程上下文中可用的内容做出假设,例如安全信息(例如,经过身份验证的主题),则也会遇到问题。如果您生成一个线程然后使用该容器未知的该线程的服务器资源,通常会发生这种情况。

Check to see if there is a way to get container managed threads from Tomcat. WebLogic and WebSphere support the commonj.WorkManager, which allows you to schedule work on container managed threads. Spring can also use commonj, but I'm not sure if that support is available on Tomcat.

检查是否有办法从Tomcat获取容器管理的线程。 WebLogic和WebSphere支持commonj.WorkManager,它允许您安排容器管理线程的工作。 Spring也可以使用commonj,但我不确定Tomcat上是否有这种支持。

#2


1  

You shouldn't really launch threads from within your webapp unless you have a very specific need to do so. Without more details on your problem it is hard to tell if this is the right approach to solve your problem.

除非您有非常具体的需要,否则不应该从Web应用程序中真正启动线程。如果没有关于您的问题的更多细节,很难说这是否是解决问题的正确方法。

You might want to take a look at Quartz, which "is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application".

您可能需要查看Quartz,它是一个功能齐全的开源作业调度系统,可以与几乎任何J2EE或J2SE应用程序集成或一起使用。

#3


1  

Your question is a bit vague. Tomcat itself already uses a thread pool to service HTTP requests. You can increase the number of threads through Tomcat configuration - look to the Tomcat wiki for info on this.

你的问题有点模糊。 Tomcat本身已经使用线程池来为HTTP请求提供服务。您可以通过Tomcat配置增加线程数 - 请查看Tomcat wiki以获取相关信息。

If you mean that in your code you want to launch threads, then I advise perusing the java.util.concurrent API introduced in Java 5. Also read "Java Concurrency in Practice", which is the text on this subject.

如果你的意思是在你的代码中想要启动线程,那么我建议仔细阅读Java 5中引入的java.util.concurrent API。另请阅读“Java Concurrency in Practice”,这是关于这个主题的文本。

#4


1  

What is the problem you are trying to solve with threads?

您尝试使用线程解决的问题是什么?

If have long running tasks you should use JMS + a full Java EE container.

如果有长时间运行的任务,则应使用JMS +完整的Java EE容器。

If you trying to handle excess load you could consider two tomcat instances, however, if you are using http sessions you will need to investigate session replication.

如果您尝试处理多余的负载,可以考虑两个tomcat实例,但是,如果您使用的是http会话,则需要调查会话复制。

If you are forced to use Tomcat consider using the Executors framework in java.util.concurrency.

如果您*使用Tomcat,请考虑在java.util.concurrency中使用Executors框架。

#5


0  

as others asked, you should give more details as to what you're trying to accomplish.

正如其他人所问,你应该提供更多关于你想要完成的事情的细节。

Otherwise, tomcat uses thread pools. increase the number of threads in the pool. Use a newer version of tomcat -- 6.x. Use Java 6.0_10. If needed, tune the application using a profiler and fiddle with the JVM settings, if required.

否则,tomcat使用线程池。增加池中的线程数。使用更新版本的tomcat - 6.x.使用Java 6.0_10。如果需要,可以使用分析器调整应用程序,并根据需要调整JVM设置。

#6


0  

The J2EE abstraction for managed multithreading is JCA. In particular, take look at the WorkManager and Work classes. See also this arcicle. Spring also provides JCA-backed work manager abstraction.

托管多线程的J2EE抽象是JCA。特别是,请查看WorkManager和Work类。另见这个穗。 Spring还提供了JCA支持的工作管理器抽象。

#1


5  

Creating your own threads inside an application server is generally discouraged because the server should manage threads for better scalability. You can also run into problems if the container makes assumptions about what's available in a thread context, such as security information (e.g., authenticated Subject). That typically happens if you spawn a thread and then use a server resource from that thread which is unknown to the container.

通常不鼓励在应用程序服务器中创建自己的线程,因为服务器应该管理线程以获得更好的可伸缩性。如果容器对线程上下文中可用的内容做出假设,例如安全信息(例如,经过身份验证的主题),则也会遇到问题。如果您生成一个线程然后使用该容器未知的该线程的服务器资源,通常会发生这种情况。

Check to see if there is a way to get container managed threads from Tomcat. WebLogic and WebSphere support the commonj.WorkManager, which allows you to schedule work on container managed threads. Spring can also use commonj, but I'm not sure if that support is available on Tomcat.

检查是否有办法从Tomcat获取容器管理的线程。 WebLogic和WebSphere支持commonj.WorkManager,它允许您安排容器管理线程的工作。 Spring也可以使用commonj,但我不确定Tomcat上是否有这种支持。

#2


1  

You shouldn't really launch threads from within your webapp unless you have a very specific need to do so. Without more details on your problem it is hard to tell if this is the right approach to solve your problem.

除非您有非常具体的需要,否则不应该从Web应用程序中真正启动线程。如果没有关于您的问题的更多细节,很难说这是否是解决问题的正确方法。

You might want to take a look at Quartz, which "is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application".

您可能需要查看Quartz,它是一个功能齐全的开源作业调度系统,可以与几乎任何J2EE或J2SE应用程序集成或一起使用。

#3


1  

Your question is a bit vague. Tomcat itself already uses a thread pool to service HTTP requests. You can increase the number of threads through Tomcat configuration - look to the Tomcat wiki for info on this.

你的问题有点模糊。 Tomcat本身已经使用线程池来为HTTP请求提供服务。您可以通过Tomcat配置增加线程数 - 请查看Tomcat wiki以获取相关信息。

If you mean that in your code you want to launch threads, then I advise perusing the java.util.concurrent API introduced in Java 5. Also read "Java Concurrency in Practice", which is the text on this subject.

如果你的意思是在你的代码中想要启动线程,那么我建议仔细阅读Java 5中引入的java.util.concurrent API。另请阅读“Java Concurrency in Practice”,这是关于这个主题的文本。

#4


1  

What is the problem you are trying to solve with threads?

您尝试使用线程解决的问题是什么?

If have long running tasks you should use JMS + a full Java EE container.

如果有长时间运行的任务,则应使用JMS +完整的Java EE容器。

If you trying to handle excess load you could consider two tomcat instances, however, if you are using http sessions you will need to investigate session replication.

如果您尝试处理多余的负载,可以考虑两个tomcat实例,但是,如果您使用的是http会话,则需要调查会话复制。

If you are forced to use Tomcat consider using the Executors framework in java.util.concurrency.

如果您*使用Tomcat,请考虑在java.util.concurrency中使用Executors框架。

#5


0  

as others asked, you should give more details as to what you're trying to accomplish.

正如其他人所问,你应该提供更多关于你想要完成的事情的细节。

Otherwise, tomcat uses thread pools. increase the number of threads in the pool. Use a newer version of tomcat -- 6.x. Use Java 6.0_10. If needed, tune the application using a profiler and fiddle with the JVM settings, if required.

否则,tomcat使用线程池。增加池中的线程数。使用更新版本的tomcat - 6.x.使用Java 6.0_10。如果需要,可以使用分析器调整应用程序,并根据需要调整JVM设置。

#6


0  

The J2EE abstraction for managed multithreading is JCA. In particular, take look at the WorkManager and Work classes. See also this arcicle. Spring also provides JCA-backed work manager abstraction.

托管多线程的J2EE抽象是JCA。特别是,请查看WorkManager和Work类。另见这个穗。 Spring还提供了JCA支持的工作管理器抽象。