声明一个boolean公共变量,表明当前httpconnection是否得到服务器回应。
  你的连接线程中在连接之前置这个变量为false;
  另起一个监视线程,拿到那个HttpConnection的连接对象,并循环监视这个boolean公共变量。如果指定时间内(20秒后)你的 boolean公共变量还是false,那么就主动置httpconnection=null.这样,那边连接线程就会抛出异常退出来。托福答案 www.jszdsy.com
  写了Timer类来实现。(学习国外一个网站上的写法)
  class Timer extends Thread {
  /** 每个多少毫秒检测一次 */
  protected int m_rate = 100;
  /** 超时时间长度毫秒计算 */
  private int m_length;
  /** 已经运行的时间 */
  private int m_elapsed;
  /**
  * 构造函数
  *
  * @param length
  * Length of time before timeout occurs
  */
  public Timer(int length) {
  // Assign to member variable
  m_length = length;
  // Set time elapsed
  m_elapsed = 0;
  }
  /**
  * 重新计时
  *
  */
  public synchronized void reset() {
  m_elapsed = 0;
  System.out.println("reset timer");
  }
  /**
  * 故意设置为超时,可以在服务器有返回,但是错误返回的时候直接调用这个,当成超时处理
  *
  */
  public synchronized void setTimeOut()
  {
  m_elapsed = m_length+1;
  }
  /**
  */
  public void run() {
  // 循环 www.yzyxedu.com
  System.out.println("timer running");
  for (;;) {
  // Put the timer to sleep
  try {
  Thread.sleep(m_rate);
  } catch (InterruptedException ioe) {
  continue;
  }
  synchronized (this) {
  // Increment time remaining
  m_elapsed += m_rate;
  // Check to see if the time has been exceeded
  if (m_elapsed > m_length && !isConnActive) { //isConnActive 为全局变量
  // Trigger a timeout
  timeout();
  break;
  }
  }
  }
  }
  /**
  * 超时时候的处理
  *
  */
  public void timeout() {
  httpConnection = null;
  System.out.println("conn time > " + TIME_OUT + " ms");
  }
  }
  在http连接线程调用的同是调用 new Timer(20*1000)。start();
  需要重新计时时候调用timer.reset();

最新文章

  1. JdbcTemplate进行查询
  2. 【原创】你知道OneNote的OCR功能吗?office lens为其增大威力,中文也识别
  3. Java 运行环境的安装、配置与运行
  4. JDBC体会
  5. ACM - 概率、期望题目 小结(临时)
  6. ProgressBarLayoutView
  7. oracle学习总结5(游标、触发器、过程、方法)
  8. C#防SQL注入代码的实现方法
  9. 练习使用css3实现3d按钮
  10. linux内核函数库文件的寻找
  11. [LeetCode]题解(python):140-Word Break II
  12. .Net程序员学用Oracle系列(5):三大数据类型
  13. tomcat证书配置
  14. java项目中通过添加filter过滤器解决ajax跨域问题
  15. Ceilometer + Aodh + Gnocchi 介绍
  16. WOW.js 使用教程
  17. 乐观锁机制解决多层嵌套异步ajax问题
  18. Java中函数的重载和重写
  19. ClsoSee(v2) Alpha测试中!这是一个临时的帮助页面...
  20. 怎么给php下拉框默认选中

热门文章

  1. openStack云平台虚拟桌面galera mysql 3节点集群实例实战 调试完成
  2. Spark 中的join方式(pySpark)
  3. 月見(つきみ) 夕(ゆう) SumiHui.墨虺
  4. SQL第二课-创建数据表
  5. app启动其他应用
  6. UML类图详细介绍
  7. jqGrid在IE中使用iframe嵌套,页码条不显示问题
  8. myNote
  9. JAVA基于AE调用GP实现泰森多边形
  10. linux 最大文件描述符fd