本文介绍了java线程池保持运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个比一个特定的问题更普通的问题。我试图有一个多线程环境保持活动,以便我可以提交任务和运行它们。我想这样做,而无需在Web服务器或应用程序服务器中执行的麻烦。想法是使用一个java线程池为此,但这里的问题是,池保持打开,直到我的main方法完成后,显然它关闭,程序完成。如何防止这种情况发生?我敢肯定有几个选项,一些比其他天真的(而真正的循环被记住)。
任何想法?谢谢。

This is more a generic question than a specific one. I'm trying to have a multi threaded environment that stays active so that I can just submit tasks and run them. I want to do this without the hassle of executing in a web server or application server. The idea was to use a java thread pool for this, but the issue here is that the pool stays open just until my main method finishes, after which obviously it closes and the program finishes. How can I prevent this from happening? I'm sure there are several options, some more naive than others (while true loops come to mind).Any ideas? Thanks.

推荐答案

如何接受您的任务?

我看到有1个线程等待或轮询任务和传递它们。这个线程将保持你的应用程序活着,也可以等待一些符号关闭应用程序,等待当前的工作完成和清理。

In many cases I saw there was 1 thread waiting or polling from tasks and passing them on. This thread will keep your application alive and can also wait for some sign to shutdown the application and the wait for the current jobs to be finished and clean up.

全部我发现处理这些应用程序生命周期事件的麻烦超过了部署到一个简单的容器(如Jetty)的麻烦很容易达到。特别是对于在后台运行的东西,我发现在一些愚蠢的JSP页面上有很多价值,以验证它仍然工作(与我们的自动监控集成)并获得一些统计信息。

All in all I find that the point where the hassle of dealing with these application lifecycle events exceed the hassle of deploying to a simple container like Jetty is easily reached. Especially for things running in the background I find a lot of value in a couple of silly JSP pages to verify it is still working (to integrate with our automatic monitoring) and getting some statistics.

这篇关于java线程池保持运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:51