java web中线程,Java Web应用程序中的线程

I need to create a thread that listens to a filepath for new, modified or removed files. I am using a web application that uses Spring 3, Struts 2 and Hibernate 3. If I run the project, it loads the index.jsp which contains the text "Hello World".

The problem is that when I create a java Thread() that listens to the filepath and assign it to index.jsp:

index.jsp

Then "Hello World" text does not ever appear because the Thread doesn't ever stop (Which is right, it's not supposed to stop). I need this Thread to be running in the background and not interfere with the rest of the web application. I don't want to create a separate web application just for this Thread though. Also, I don't want a new Thread instance to be created each time a new Session instance is created, there should be just one Thread instance no matter how many Session instances there are active.

解决方案

Basically, people will suggest not to start your own thread in a container managed application, like a web application or an enterprise (EJB) application.

This is because in a container environment all the resources including threads should be managed by container. But IMHO if you know the implications, and the life cycle of our own thread and its resources, like wrapping this thread with a transaction etc, then we can create a thread by using the standard way (new Thread()), but it is just my opinion.

I would suggest you to go through the 'Container managed threads' once before you make a decision. You can refer commonJ once to get a handle to Container's thread pool, but as this is not a standard or not mentioned in the container specifications, It may not work for all the containers. This will work for:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值