
java多线程问题请高手解决
我配置了一个servlet监听器。在监听器里面开启一个线程,为什么线程会运行三次啊,在main方法里面测试这个线程没问题的。。高手赐教。。小弟在线等。。publiccla...
我配置了一个servlet监听器。在监听器里面开启一个线程,为什么线程会运行三次啊,在main方法里面测试这个线程没问题的。。高手赐教。。小弟在线等。。
public class OpenGameThreads implements ServletContextListener{
/**
* 监听器
*/
public void contextInitialized(ServletContextEvent sce) {
GameThreads th = new GameThreads();
synchronized (th) {
new Thread(th).start();//启动线程
}
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
public class GameThreads extends Thread{
UserTop ut = new UserTop();
public void run()
{
synchronized (this) {
ut.GameStatistics();//调用方法统计方法
try {
Thread.currentThread().sleep(300000);//设置线程休眠时间以毫秒计算(5分钟)
System.out.println("线程运行次数玩家同时在线数据");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
<!-- 配置服务器监听 实现玩家同时在线数据统计-->
<listener>
<listener-class>org.gameboo.threads.OpenGameThreads</listener-class>
</listener> 展开
public class OpenGameThreads implements ServletContextListener{
/**
* 监听器
*/
public void contextInitialized(ServletContextEvent sce) {
GameThreads th = new GameThreads();
synchronized (th) {
new Thread(th).start();//启动线程
}
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
public class GameThreads extends Thread{
UserTop ut = new UserTop();
public void run()
{
synchronized (this) {
ut.GameStatistics();//调用方法统计方法
try {
Thread.currentThread().sleep(300000);//设置线程休眠时间以毫秒计算(5分钟)
System.out.println("线程运行次数玩家同时在线数据");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
<!-- 配置服务器监听 实现玩家同时在线数据统计-->
<listener>
<listener-class>org.gameboo.threads.OpenGameThreads</listener-class>
</listener> 展开
1个回答
展开全部
我觉得没什么问题啊,我的就是这样写的,最好在contextInitialized中输出一下,看看初始化了几次?要是多次的话,是不是你的容器时热部署啊?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询