我现在在做一个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();
}
}
以上是我写的程序,结论是捕获不到

解决方案 »

  1.   

    Android提供了Handler、Looper、Message等类,可以很容易实现你说的timeout功能。
      

  2.   

    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()   楼主要的输出结果是可以实现的;此外   我 用了另外一种方法   进行线程间消息传递     :  用了一个标记位;
    不知道符不符合楼主的心意,先这样吧!!
      

  3.   

    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 ()    就可以了   貌似线程间异常无法传递啊!!!
    不知道符不符合楼主心意,呵呵!!!
      

  4.   

    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");
        }
      
    }我勒个去  想了一个不太好的方法  实现了进程间的异常传递;
    貌似可以使用!!!程序关键点:睡眠等待   调用线程等待时间长于被掉线程;
      

  5.   

    观察者模式:JDK实现Observable与Observer
      

  6.   


    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();
        }
    }