我可以从Servlet产生线程吗? [英] Can i spawn a thread from a servlet?
问题描述
在开始我的主要问题之前,我想问一个基本问题.
I would like to ask a basic question before i get on to my main question .
可以说我正在运行一个简单的Java程序,该程序在main函数中产生一个线程.当主要功能完成时,线程会继续运行吗? 线程之间是否有父/子关系的概念.
Lets say i am running a simple Java program, which spawns a thread in the main function. Will the thread continue to run when the main function completes? Is there a concept of parent/child relationships between threads.
我有一个Servlet,需要很长时间才能处理请求(5分钟).我可以从主servlet中产生一个后台线程来处理工作吗?很快回来.即使主servlet完成处理后,后台线程也会继续运行吗?
I have a servlet which takes a long while to process a request (5 mins). Can i spawn a background thread from the main servlet to handle the work & return soon. Would the background thread keep running even when the main servlet has finished processing?
推荐答案
-
有诸如父线程和子线程之类的东西,但是您对此没有太多的控制权.例如,存在
InheritableThreadLocal
,您可以在其中存储线程层次结构的变量.There is such thing as parent and child threads, but you don't have a lot of control on that. For example there's
InheritableThreadLocal
, where you can store variables for a thread hierarchy.您可以从servlet产生一个新线程.首选Java 5执行程序框架
you can spawn a new thread from a servlet. Prefer Java 5 executors framework
if using servlet 3.0, take a look at its asynchronous processing capabilities.
这篇关于我可以从Servlet产生线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!