- 浏览: 230342 次
- 性别:
- 来自: 海南海口
-
文章分类
- 全部博客 (114)
- java基础 (25)
- 设计模式 (6)
- css (1)
- js (2)
- jquery (5)
- flash as3.0 (3)
- lucene (2)
- tomcat (3)
- uml (0)
- struts2 (7)
- spring (0)
- sql (1)
- ejb3.0 (2)
- jbpm4 (1)
- webservices (1)
- linux (3)
- ajax (1)
- 面试 (1)
- flex (0)
- soa (0)
- oracle解锁 (5)
- 工具 (3)
- ext (3)
- 好的网址 (1)
- junit (2)
- jmx (2)
- encache (1)
- redis (1)
- 网站 (1)
- oracle重要的sql (1)
- web (3)
- hadoop (2)
- DB2 (1)
- ui (1)
- sybase (1)
- ue使用快捷键 (1)
- eclipse优化 (1)
- 前端优化用到的插件 (1)
- zookeeper (1)
- solr (1)
- hibernate (1)
- svn (1)
- resion (1)
- resin (1)
- maven (1)
- mysql (1)
- url (1)
- 通过HttpFileServer设置共享 可以通过http方式访问 (1)
- 非技术 (2)
- 营销 (1)
- ELK (3)
最新评论
-
it_xiaowu:
jqwerty_123 写道我的出同样的问题却是因为引入cxf ...
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Ma -
繁星水:
实验证明可用,最后补充一下,可以不需要 set Package ...
axis根据wsdl生成java客户端代码 -
qq_16699317:
qq_16699317 写道求一份源代码,感激不尽。。。多谢了 ...
java博客系统 -
qq_16699317:
求一份源代码,感激不尽。。。多谢了
java博客系统 -
jqwerty_123:
我的出同样的问题却是因为引入cxf的时候jcl-over-sl ...
java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Ma
package cn.mytest;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @Description: 线程监控超时的工具类
* @author
* @date 2014-9-18 下午04:47:12
*/
public class ThreadWathcher extends Thread {
private static ThreadWathcher watcher;
/**
* 存放对应的线程跟开始执行的时间
*/
private HashMap<Thread, Long> threadBornTimeCollection;
/**
* 需要被中断的线程
*/
private ArrayList<Thread> toRemoveThreads;
/**
* 超时时间
*/
private long timeOutMills;
/**
* 间隔扫描时间
*/
private long periodMills;
private ThreadWathcher() {
/**
* 设置线程为守护线程 以致主线程停止的时候守护线程也自动终止
*/
this.setDaemon(true);
threadBornTimeCollection = new HashMap<Thread, Long>();
toRemoveThreads = new ArrayList<Thread>();
}
/**
* 配置的超时时间必须是间隔检测时间的倍数+3 比如超时时间是1000则 period应该是503
* @param timeout
* @param period
* @return
*/
public static ThreadWathcher getInstance(long timeout, long period) {
if (watcher == null) {
watcher = new ThreadWathcher();
watcher.timeOutMills = timeout;
watcher.periodMills = period;
}
return watcher;
}
public int register(Thread thread) {
threadBornTimeCollection.put(thread, System.currentTimeMillis());
return threadBornTimeCollection.size();
}
@Override
public void run() {
super.run();
while (true) {// 守护线程
try {
Thread.sleep(periodMills);// 每隔periodMills秒检查一次
for (Thread e : threadBornTimeCollection.keySet()) {// 遍历已经注册过超时处理的线程集合
if (Math.abs(threadBornTimeCollection.get(e)
- System.currentTimeMillis()) > timeOutMills
&& e.isAlive()) {// 超时
toRemoveThreads.add(e);// 添加到超時线程集合中
}
}
for (Thread e : toRemoveThreads) {// 遍历超时线程集合
threadBornTimeCollection.remove(e);// 从超时集合中移除
e.interrupt();// 中断超时线程
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (toRemoveThreads.size() > 0) {
System.out.println("清空超时线程集合");
toRemoveThreads.clear();// 清空超时线程集合
}
}
}
}
}
package cn.mytest;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Begin here...");
ThreadWathcher wacher = ThreadWathcher.getInstance(1000, 503);
wacher.start();
Thread a = new Thread() {
@Override
public void run() {
super.run();
int n = 0;
try {
System.out.println("A线程执行中-----");
Thread.sleep(1100);
System.out.println("A线程执行完成.....");
/* while (n < 1 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread a..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
a.setName("a");
wacher.register(a);
a.start();
Thread b = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("B线程执行中-----");
Thread.sleep(900);
System.out.println("B线程执行完成.....");
/* while (n < 5 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread b..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
b.setName("b");
b.start();
wacher.register(b);
Thread c = new Thread() {
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("C线程执行中-----");
Thread.sleep(1200);
System.out.println("C线程执行完成.....");
/* while (n < 12 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread c..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
c.setName("c");
c.start();
wacher.register(c);
Thread d = new Thread() {
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("D线程执行中-----");
Thread.sleep(500);
System.out.println("D线程执行完成.....");
/* while (n < 15 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread d..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
d.setName("d");
d.start();
wacher.register(d);
}
}
import java.util.ArrayList;
import java.util.HashMap;
/**
* @Description: 线程监控超时的工具类
* @author
* @date 2014-9-18 下午04:47:12
*/
public class ThreadWathcher extends Thread {
private static ThreadWathcher watcher;
/**
* 存放对应的线程跟开始执行的时间
*/
private HashMap<Thread, Long> threadBornTimeCollection;
/**
* 需要被中断的线程
*/
private ArrayList<Thread> toRemoveThreads;
/**
* 超时时间
*/
private long timeOutMills;
/**
* 间隔扫描时间
*/
private long periodMills;
private ThreadWathcher() {
/**
* 设置线程为守护线程 以致主线程停止的时候守护线程也自动终止
*/
this.setDaemon(true);
threadBornTimeCollection = new HashMap<Thread, Long>();
toRemoveThreads = new ArrayList<Thread>();
}
/**
* 配置的超时时间必须是间隔检测时间的倍数+3 比如超时时间是1000则 period应该是503
* @param timeout
* @param period
* @return
*/
public static ThreadWathcher getInstance(long timeout, long period) {
if (watcher == null) {
watcher = new ThreadWathcher();
watcher.timeOutMills = timeout;
watcher.periodMills = period;
}
return watcher;
}
public int register(Thread thread) {
threadBornTimeCollection.put(thread, System.currentTimeMillis());
return threadBornTimeCollection.size();
}
@Override
public void run() {
super.run();
while (true) {// 守护线程
try {
Thread.sleep(periodMills);// 每隔periodMills秒检查一次
for (Thread e : threadBornTimeCollection.keySet()) {// 遍历已经注册过超时处理的线程集合
if (Math.abs(threadBornTimeCollection.get(e)
- System.currentTimeMillis()) > timeOutMills
&& e.isAlive()) {// 超时
toRemoveThreads.add(e);// 添加到超時线程集合中
}
}
for (Thread e : toRemoveThreads) {// 遍历超时线程集合
threadBornTimeCollection.remove(e);// 从超时集合中移除
e.interrupt();// 中断超时线程
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (toRemoveThreads.size() > 0) {
System.out.println("清空超时线程集合");
toRemoveThreads.clear();// 清空超时线程集合
}
}
}
}
}
package cn.mytest;
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Begin here...");
ThreadWathcher wacher = ThreadWathcher.getInstance(1000, 503);
wacher.start();
Thread a = new Thread() {
@Override
public void run() {
super.run();
int n = 0;
try {
System.out.println("A线程执行中-----");
Thread.sleep(1100);
System.out.println("A线程执行完成.....");
/* while (n < 1 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread a..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
a.setName("a");
wacher.register(a);
a.start();
Thread b = new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("B线程执行中-----");
Thread.sleep(900);
System.out.println("B线程执行完成.....");
/* while (n < 5 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread b..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
b.setName("b");
b.start();
wacher.register(b);
Thread c = new Thread() {
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("C线程执行中-----");
Thread.sleep(1200);
System.out.println("C线程执行完成.....");
/* while (n < 12 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread c..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
c.setName("c");
c.start();
wacher.register(c);
Thread d = new Thread() {
public void run() {
// TODO Auto-generated method stub
super.run();
int n = 0;
try {
System.out.println("D线程执行中-----");
Thread.sleep(500);
System.out.println("D线程执行完成.....");
/* while (n < 15 && !Thread.interrupted()) {
Thread.sleep(1000);
n++;
System.out.println("In thread d..." + n);
} */
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("线程因超时被终止...线程名" + this.getName());
}
}
};
d.setName("d");
d.start();
wacher.register(d);
}
}
发表评论
-
java调用cmd不显示cmd窗口的
2014-07-11 09:34 1129cmd /c start /b fis release 加 ... -
hadoop的入门案例
2013-03-20 17:03 1012http://www.cnblogs.com/xia520pi ... -
数据库字段内容的压缩算法
2013-03-08 14:34 1085package cn.ljz; import java. ... -
记录一下消息异步发送的一下框架
2013-02-03 19:02 967disruptor jgroups akka Activ ... -
eclipse中调试jvm内存溢出
2013-01-26 19:45 1333http://www.blogjava.net/rosen/a ... -
jvm退出之前清理工作
2013-01-24 23:25 881try { Runtime.getRu ... -
jvm内存泄露
2012-12-14 14:22 1098查看java内存泄露的办法 1:利用jdk自带的jps命令查看 ... -
Java获取当前运行方法的名称
2012-06-04 14:42 1555方法一:new Exception().getStackTra ... -
jdbc封装事务
2012-02-27 10:10 1184装载从这个网址过来的http://hi.baidu.com/g ... -
hashmap源码解析经典
2011-12-30 22:24 1039http://www.java3z.com/cwbwebhom ... -
hashmap的遍历
2011-12-30 22:04 1084for(Iterator ite = map.entrySet ... -
axis根据wsdl生成java客户端代码
2011-12-24 16:30 14383先下载axis jar包:axis-bin-1_4.zip。下 ... -
eclipse设置编译路径
2011-12-24 15:40 1114ljzblog/WebRoot/WEB-INF/classes ... -
java反射判断数组
2011-12-17 19:52 1121if (c.isArray()) { String ca ... -
java方法参数注解
2011-12-03 21:46 16074package cn.ljz.annotation; i ... -
java获取项目路径
2011-08-16 09:19 690this.getClass().getClassLoader( ... -
CardLayout 简单实现Demo
2010-11-10 00:11 2498package cn.ljz.test; import ... -
java反射机制小小总结
2010-08-18 10:40 12941:动态语言就是在程序运行的过程当中,可以改变程序的结果和变量 ... -
java线程小小总结
2010-08-18 09:52 1117线程:简单的来说就是 ... -
java注解小总结
2010-08-18 09:46 18951:从jdk1.5以后就开始出现注解了,主要有@overrid ...
评论