java 线程之间传递异常 懂的进!

genguyige 2012-03-01 08:51:43
我现在在做一个android的程序
思路如下:假设我的程序连接服务器时间太长,或者根本不能连接到服务器,那么我规定在一定的时间比如3s或者5s之后我的程序就弹出一个不能连接的框

那么我要写一个定时器,思路是用一个守护线程,从连接之前start();让其sleep到我规定的时间,如果这段时间还没有连接成功,就报一个TimeoutException异常

但问题是,java的异常机制是向上捕获。也就是说会一层一层的向上去请求处理,没有说在线程之间可以捕获到的

那么问题来了1:如果我的这种方法不可行,请有做过类似东西的人给其他方案
2:有么有方法可以让主线程捕获到守护线程的异常

public class Test1 extends Thread{
public boolean isCanceled;
@Override
public void run() throws NullPointerException{
throw new NullPointerException();
}
}


public class Test2 extends Thread{

@Override
public void run(){
try{
new Test1().start();
}catch(NullPointerException e){
System.out.println("helloworld");
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test2().start();
}
}


以上是我写的程序,结论是捕获不到
...全文
297 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
genguyige 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dntg007 的回复:]

有个概念你要清楚。调用一个新线程之后,主线程不会等待,会继续执行后面的代码。以你的愿意。4秒后才能判断是否异常,那时候主线程都不知道走哪去了。这种思路本身就是错的。
[/Quote]

恩这种思路的确有问题,因为如果没有线程出现报异常的话,主线程就去了别的地方,而我新开的线程就找不到了,没办法结束,还是会在后台运行
dntg007 2012-03-07
  • 打赏
  • 举报
回复
有个概念你要清楚。调用一个新线程之后,主线程不会等待,会继续执行后面的代码。以你的愿意。4秒后才能判断是否异常,那时候主线程都不知道走哪去了。这种思路本身就是错的。
huangxw000 2012-03-07
  • 打赏
  • 举报
回复

package callBack;

public interface TimeOut {
public void say();
}


package callBack;

[code=Java]public class ThreadCount implements Runnable {

private ThreadDo threadDo = null;

public ThreadCount() {
super();
}

public ThreadCount(ThreadDo threadDo) {
super();

this.threadDo = threadDo;
}

@Override
public void run() {
synchronized (this) {
while (true) {
try {
this.wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.threadDo.setFlag(true);
break;
}
}
}
}



package callBack;

public class ThreadDo implements Runnable {

private boolean flag = false;
private TimeOut timeOut = null;

public ThreadDo() {
super();
}

public ThreadDo(boolean flag, TimeOut timeOut) {
super();
this.flag = flag;
this.timeOut = timeOut;
}

public TimeOut getTimeOut() {
return timeOut;
}

public void setTimeOut(TimeOut timeOut) {
this.timeOut = timeOut;
}

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

@Override
public void run() {
synchronized (this) {
while (true) {
this.notifyAll();
if (this.flag) {
this.doSth();
break;
}
}
}
}

public void doSth() {
timeOut.say();
}

}[/code]


package callBack;

public class Test {
public static void main(String[] args) {
ThreadDo threadDo = new ThreadDo();
threadDo.setTimeOut(new TimeOut() {
@Override
public void say() {
System.out.println("time out!");
}
});
ThreadCount threadCount = new ThreadCount(threadDo);
new Thread(threadCount).start();
new Thread(threadDo).start();
}
}
huangxw000 2012-03-07
  • 打赏
  • 举报
回复
观察者模式:JDK实现Observable与Observer
xiaoliuliu2050 2012-03-07
  • 打赏
  • 举报
回复
public class testAbnormalDeliever {
public static void main(String a[]){
Test1 x=new Test1();
new Test2(x).start();
}
}
class Test1 extends Thread{
public boolean isCanceled;
Exception ex=null;
@Override
public void run()throws NullPointerException {
try {
sleep(5000);//等待5秒
if(true){ //这里条件是没连接上
ex=new NullPointerException();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


class Test2 extends Thread{
public Test1 x=null;

Test2(Test1 x){
this.x=x;
}
public void run(){
try{
x.start(); //线程启动
try {
sleep(6000); //等待6秒钟
} catch (InterruptedException e) {

e.printStackTrace();
}
if(x.ex!=null) //
throw (NullPointerException)x.ex;
}catch(NullPointerException e){
System.out.println("helloworld");
e.printStackTrace();
}
System.out.print("ssssssssssssssssss");
}

}



我勒个去 想了一个不太好的方法 实现了进程间的异常传递;
貌似可以使用!!!

程序关键点:

睡眠等待 调用线程等待时间长于被掉线程;

xiaoliuliu2050 2012-03-07
  • 打赏
  • 举报
回复
public class testAbnormalDeliever {
public static void main(String a[]){
Test1 x=new Test1();
new Test2(x).start();
}

}
class Test1 extends Thread{
public boolean flag=false;
@Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if没有连接成功 则标记为flag为true;
flag=true;
}
}


class Test2 extends Thread{
public Test1 x=null;

Test2(Test1 x){
this.x=x;
}
public void run(){

x.start();
if(x.flag==true){
System.out.print("连接不成功");

}
}

}

用一个标记位 也可以进行 进程间 消息传递;



此外楼主的程序 把start() 换成 run () 就可以了

貌似线程间异常无法传递啊!!!
不知道符不符合楼主心意,呵呵!!!
xiaoliuliu2050 2012-03-07
  • 打赏
  • 举报
回复
public class testAbnormalDeliever {
public static void main(String a[]){
Test1 x=new Test1();
new Test2(x).start();
}

}
class Test1 extends Thread{
public boolean flag=false;
@Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if没有连接成功 则标记为flag为true;
flag=true;
}
}


class Test2 extends Thread{
public Test1 x=null;

Test2(Test1 x){
this.x=x;
}
public void run(){

x.start();
if(x.flag==true){
System.out.print("连接不成功");

}
}

}



貌似线程间异常传递不可实现 ,不过楼主的程序,如果把start() 函数 都换成 run() 楼主要的输出结果是可以实现的;





此外 我 用了另外一种方法 进行线程间消息传递 : 用了一个标记位;




不知道符不符合楼主的心意,先这样吧!!
nj_dobetter 2012-03-07
  • 打赏
  • 举报
回复
Android提供了Handler、Looper、Message等类,可以很容易实现你说的timeout功能。
KG071 2012-03-01
  • 打赏
  • 举报
回复
1,如楼上所说,回调
2,不一定非要用在主线程捕获异常这种方法,可以在守护线程里发现5s得不到连接之后,触发某个方法
aotian16 2012-03-01
  • 打赏
  • 举报
回复
做个钩子函数回调不行吗

62,612

社区成员

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

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