JavaWeb 项目启动时,后台开启一个线程的方法
原文鏈接:http://blog.csdn.net/it_wangxiangpan/article/details/7168286
JavaWeb 服務(wù)啟動(dòng)時(shí),在后臺(tái)啟動(dòng)加載一個(gè)線程。
目前,我所掌握的一共有兩種方法,第一種是監(jiān)聽(Listener),第二種是配置隨項(xiàng)目啟動(dòng)而啟動(dòng)的Servlet。
下面對(duì)這兩種方法做一簡單的介紹,(Mark一下,防止以后急用又忘記了):
監(jiān)聽(Listener)
首先,我們創(chuàng)建一個(gè)監(jiān)聽的類,繼承ServletContextListener,如下:
package com.wxp.thread; ?
import javax.servlet.ServletContextEvent; ?
import javax.servlet.ServletContextListener; ?
/**
* Listener的方式在后臺(tái)執(zhí)行一線程
*
* @author Champion.Wong
*
*/
publicclass MyListener implements ServletContextListener { ?
private MyThread myThread; ?
publicvoid contextDestroyed(ServletContextEvent e) { ?
if (myThread != null && myThread.isInterrupted()) { ?
? ? ? ? ? ?myThread.interrupt(); ?
? ? ? ?} ?
? ?} ?
publicvoid contextInitialized(ServletContextEvent e) { ?
? ? ? ?String str = null; ?
if (str == null && myThread == null) { ?
? ? ? ? ? ?myThread = new MyThread(); ?
? ? ? ? ? ?myThread.start(); // servlet 上下文初始化時(shí)啟動(dòng) socket
? ? ? ?} ?
? ?} ?
} ?
/**
* 自定義一個(gè) Class 線程類繼承自線程類,重寫 run() 方法,用于從后臺(tái)獲取并處理數(shù)據(jù)
*
* @author Champion.Wong
*
*/
class MyThread extends Thread { ?
publicvoid run() { ?
while (!this.isInterrupted()) {// 線程未中斷執(zhí)行循環(huán)
try { ?
? ? ? ? ? ? ? ?Thread.sleep(2000); //每隔2000ms執(zhí)行一次
? ? ? ? ? ?} catch (InterruptedException e) { ?
? ? ? ? ? ? ? ?e.printStackTrace(); ?
? ? ? ? ? ?} ?
// ? ? ? ? ? ------------------ 開始執(zhí)行 ---------------------------
? ? ? ? ? ?System.out.println("____FUCK TIME:" + System.currentTimeMillis()); ?
? ? ? ?} ?
? ?} ?
} ?
然后,在web.xml中配置如下:
[html]view plaincopy
<listener>
<listener-class>com.wxp.thread.MyListener</listener-class>
</listener>
OK,啟動(dòng)項(xiàng)目,我們可以在控制臺(tái)看到隔時(shí)間段輸出的文字內(nèi)容。
使用Servlet,在項(xiàng)目啟動(dòng)的時(shí)候啟動(dòng)它。
首先,創(chuàng)建一個(gè)Servlet,繼承HttpServlet
package com.wxp.thread; ?
import java.io.IOException; ?
import javax.servlet.ServletException; ?
import javax.servlet.http.HttpServlet; ?
import javax.servlet.http.HttpServletRequest; ?
import javax.servlet.http.HttpServletResponse; ?
import com.ite.common.Constants; ?
publicclass MyServlet extends HttpServlet{ ?
privatestaticfinallong serialVersionUID = 1L; ?
private MyThread1 myThread1; ?
public MyServlet(){ ?
? ?} ?
publicvoid init(){ ?
? ? ? ?String str = null; ?
if (str == null && myThread1 == null) { ?
? ? ? ? ? ?myThread1 = new MyThread1(); ?
? ? ? ? ? ?myThread1.start(); // servlet 上下文初始化時(shí)啟動(dòng) socket
? ? ? ?} ?
? ?} ?
publicvoid doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) ?
throws ServletException, IOException{ ?
? ?} ?
publicvoid destory(){ ?
if (myThread1 != null && myThread1.isInterrupted()) { ?
? ? ? ? ? ?myThread1.interrupt(); ?
? ? ? ?} ?
? ?} ?
} ?
/**
* 自定義一個(gè) Class 線程類繼承自線程類,重寫 run() 方法,用于從后臺(tái)獲取并處理數(shù)據(jù)
*
* @author Champion.Wong
*
*/
class MyThread1 extends Thread { ?
publicvoid run() { ?
while (!this.isInterrupted()) {// 線程未中斷執(zhí)行循環(huán)
try { ?
? ? ? ? ? ? ? ?Thread.sleep(2000); ?
? ? ? ? ? ?} catch (InterruptedException e) { ?
? ? ? ? ? ? ? ?e.printStackTrace(); ?
? ? ? ? ? ?} ?
// ------------------ 開始執(zhí)行 ---------------------------
? ? ? ? ? ?System.out.println("____FUCK TIME:" + System.currentTimeMillis()); ?
? ? ? ?} ?
? ?} ?
} ?
然后,在web.xml中配置
[html]view plaincopy
<!-- LISTENER FOR THREAD -->
<servlet>
<servlet-name>MyListener</servlet-name>
<servlet-class>com.ite.wxp.MyServlet</servlet-class>
<load-on-startup>9</load-on-startup><!-- 數(shù)字越小,啟動(dòng)的優(yōu)先級(jí)越高,必須大于0 -->
</servlet>
<servlet-mapping>
<servlet-name>MyListener</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
OK。啟動(dòng)項(xiàng)目,依然可以看到如圖所示:
? ? ? ? ? ? ? ? ? ? ? ? ? 其中,Listener的方式,該線程肯定是項(xiàng)目中首先啟動(dòng)的,優(yōu)先于任何一個(gè)Servlet。而Servlet的方式,可以設(shè)置與其它Servlet啟動(dòng)的順序。如果有時(shí)候需要首先啟動(dòng)一個(gè)Servlet或者它們之間的啟動(dòng)順序有特殊要求的時(shí)候,這個(gè)就很有作用了。
轉(zhuǎn)載于:https://blog.51cto.com/jormin/1358552
總結(jié)
以上是生活随笔為你收集整理的JavaWeb 项目启动时,后台开启一个线程的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 并发编程(一): POSIX 使用互斥量
- 下一篇: java美元兑换,(Java实现) 美元