下边是我写的一个 测试线程 :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()方法已经结束,也就是没有死亡.我想到了另外一个方法,就是不在本线程中 设置重启,另起一个伺服线程 每隔 一段时间 判断本线程是否存活,这样可以实现,但是 一起启动两个伺服线程 我认为很耗内存 ,不知各位高人 有没有好的解决办法 启动一个伺服线程就能解决 意外死亡自动重启 的问题。先谢过了
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()方法已经结束,也就是没有死亡.我想到了另外一个方法,就是不在本线程中 设置重启,另起一个伺服线程 每隔 一段时间 判断本线程是否存活,这样可以实现,但是 一起启动两个伺服线程 我认为很耗内存 ,不知各位高人 有没有好的解决办法 启动一个伺服线程就能解决 意外死亡自动重启 的问题。先谢过了
解决方案 »
- 如何比较两个Date对象哪个时间早晚?(本人初学,很小白的问题)
- JTree如何展开与收缩
- 一个关于java数组交换值的问题!谁能帮帮我
- JAVA前景怎么样?~
- 寝食难安农业的毕业设计问题:如何解决JTree中的选择事件,请大家一定看看,谢谢!
- DataOutputStream的readInt方法能否批量读入文件内容到int数组?
- 如何得到一个文件夹的大小,并知道所在盘符的剩余空间
- 为什么我安装的JDK显现中文的地方都是方块(在WIN2K)
- Java数组问题
- 关于数据库操作问题,添加记录没问题,但是查询就报异常!求大神解决!
- 应该选择使用哪一种布局管理器呢?
- 声明为 static和private 的方法实际上就是final的。这句话无法理解!!
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();
}
} }
}
public void resetThread1(){
createTread1();
}
就ok了被
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 都不是推荐方法 但是我么办法了
....
}
catch(Throwable ex){
resetThread1();
}