关于线程 意外死亡 自动重启的问题

SuperBoo 2010-03-30 09:30:51
下边是我写的一个 测试线程 :

public class TestThread {
private Thread thread1;

public static void main(String[] args) {
TestThread t = new TestThread();
t.createTread1();
}
/*
* 启动线程
*/
public void createTread1(){
thread1 = new Thread(new ThreadI());
thread1.start();
}
/*
* 判断线程是否死亡,重启线程
*/
public void resetThread1(){
System.out.println("是否存活: "+thread1.isAlive());
if(!thread1.isAlive())
{
createTread1();
}
}
/*
* 伺服线程
*/
class ThreadI implements Runnable {
private int temp = 0;
public void run() {
try{
while (true) {
try {
if(temp>=10)
throw new RuntimeException();
System.out.println("------------循环显示中---------");
Thread.sleep(2000);
temp++;
} catch (Exception e) {
break;
}
}
}finally{
System.out.println("后判断 是否存活");
//try {
// Thread.currentThread().join();
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
resetThread1();
}

}

}
}


以上程序 是我初步的设想 但是这样不能达到我的要求 因为当在finally块中调用 resetThread1方法是 线程并不认为run()方法已经结束,也就是没有死亡.

我想到了另外一个方法,就是不在本线程中 设置重启,另起一个伺服线程 每隔 一段时间 判断本线程是否存活,这样可以实现,但是 一起启动两个伺服线程 我认为很耗内存 ,不知各位高人 有没有好的解决办法 启动一个伺服线程就能解决 意外死亡自动重启 的问题。先谢过了
...全文
342 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ark032425 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 superboo 的回复:]
Java code

private Thread thread1;

public static void main(String[] args) {
TestThread t = new TestThread();
t.createTread1();
}
/*
* 启动线程
*/
……
[/Quote]
抛异常跳出循环就可以认为现程步工作了(不管死不死;反正步工作了。)
就可以开个新现成;老的那个可以自己销毁掉了
「已注销」 2010-03-30
  • 打赏
  • 举报
回复
try{
....
}
catch(Throwable ex){
resetThread1();
}
SuperBoo 2010-03-30
  • 打赏
  • 举报
回复

private Thread thread1;

public static void main(String[] args) {
TestThread t = new TestThread();
t.createTread1();
}
/*
* 启动线程
*/
public void createTread1(){
thread1 = new Thread(new ThreadI());
thread1.start();
}
/*
* 判断线程是否死亡,重启线程
*/
public void resetThread1(){
thread1=null;
createTread1();
}
/*
* 伺服线程
*/
class ThreadI implements Runnable {
private int temp = 0;
public void run() {
try{
while (true) {
try {
if(temp>=10)
throw new RuntimeException();
System.out.println("------------循环显示中---------");
Thread.sleep(2000);
temp++;
} catch (Exception e) {
break;
}
}
}finally{
System.out.println("后判断 是否存活");
resetThread1();
}

}

}
}



改成这样了 虽然用 stop() destroy() null 都不是推荐方法 但是我么办法了
SuperBoo 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 superboo 的回复:]

引用 3 楼 ark032425 的回复:

看看是否符合你的要求

你这样处理 完全不管 线程是否真正死亡 就 另起了一个新的线程
[/Quote]
这么说吧 如果 按着这种要求 我连状态位都不用 直接调用
public void resetThread1(){
createTread1();
}
就ok了被
SuperBoo 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ark032425 的回复:]

看看是否符合你的要求
[/Quote]
你这样处理 完全不管 线程是否真正死亡 就 另起了一个新的线程
Ark032425 2010-03-30
  • 打赏
  • 举报
回复
看看是否符合你的要求
Ark032425 2010-03-30
  • 打赏
  • 举报
回复

public class TestThread {
private Thread thread1;
boolean flag = false;//加个标志位

public static void main(String[] args) {
TestThread t = new TestThread();
t.createTread1();
}
/*
* 启动线程
*/
public void createTread1(){
thread1 = new Thread(new ThreadI());
thread1.start();
}
/*
* 判断线程是否死亡,重启线程
*/
public void resetThread1(){
System.out.println("是否存活: "+thread1.isAlive());
if(flag) //用标志位判断
{
createTread1();
falg = false; //创建好后再置回false;以便下次再用
}
}
/*
* 伺服线程
*/
class ThreadI implements Runnable {
private int temp = 0;
public void run() {
try{
while (true) {
try {
if(temp>=10)
throw new RuntimeException();
System.out.println("------------循环显示中---------");
Thread.sleep(2000);
temp++;
} catch (Exception e) {
break;
}
}
}finally{
System.out.println("后判断 是否存活");
flag = true;// 结束之前把标志位置true
//try {
// Thread.currentThread().join();
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
resetThread1();
}

}

}
}

SuperBoo 2010-03-30
  • 打赏
  • 举报
回复
我顶起

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧