线程超时

  1. //    SearchThread.java  
  2. class SearchThread extends Thread {  
  3.        private List result = new ArrayList();  
  4.        private boolean success_flag = false;  
  5.        ...........  
  6.   
  7.        public SearchThread(xxxx,xxxxx) {  
  8.             .......  
  9.        }  
  10.   
  11.        public void run() {  
  12.              this.result = API.getSearchResult(xxxx,xxxx,xxxx);  
  13.              this.success_flag = true;  
  14.        }  
  15.  
  16.        public List getResults() {  
  17.              return this.result;  
  18.        }  
  19.   
  20.        public boolean isSuccess() {  
  21.              return this.success_flag;  
  22.        }  
  23. }  
  1. //  XXXAction.java  
  2. ...........  
  3. SearchThread thread = new SearchThread(xxxxxx,  xxxxxxx);  
  4. thread.start();  
  5. try {  
  6.      thread.join(MAX_WAIT_TIME);  
  7.      if (thread.isSuccess()) {  
  8.           result_lst = thread.getResults();  
  9.      } else {  
  10.           throw new DataBaseTimeoutException();  
  11.      }  
  12. catch (Exception e) {  
  13.      thread.interrupt();  
  14.      ........  
  15. }  
  16. MAX_WAIT_TIME就是最大等待时间20秒,如果join()到达最长等待时间,但 是SearchThread里面的success_flag还没有被设置成true的时候,就是说明已经超时,检索没有完成,这个时候会抛出自定义的 DataBaseTimeoutException,中断线程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值