线程超时,服务进入假死状态
2017-09-04 00:00
288 查看
摘要: 最近遇到一个关于线程超时,服务进入假死的问题
项目最近写了一个接口,设定不断的去请求第三方获得数据,由于网络等等原因,httpClient请求偶尔会超时,超时后进入假死状态,卡死。查询了一下好像是httpClient 3.x存在这种问题,在4.x后已经修复。
如果想一个别的路子:譬如,捕捉每一次httpClient请求,判断该线程的执行时间,如果超过常规值则将该线程杀掉,下面的工作则可以正常进行了。
搜罗一下有两种可行的方案。
一、CounterThread.join()方法
二、ExecutorService.submit(Thread).get()
此方法经验证是我最喜欢的方法。
项目最近写了一个接口,设定不断的去请求第三方获得数据,由于网络等等原因,httpClient请求偶尔会超时,超时后进入假死状态,卡死。查询了一下好像是httpClient 3.x存在这种问题,在4.x后已经修复。
如果想一个别的路子:譬如,捕捉每一次httpClient请求,判断该线程的执行时间,如果超过常规值则将该线程杀掉,下面的工作则可以正常进行了。
搜罗一下有两种可行的方案。
一、CounterThread.join()方法
import java.util.concurrent.*; /** * Created by liyang on 2017/9/4. */ public class ThreadTest { public static void main(String[] args) { CounterThread ct = new CounterThread(1314); ct.start(); try { ct.join(5000); //超时的时间,如5秒 if(ct.isAlive()){//说明再设定的时间内没有执行完,超时 ct.interrupt(); throw new TimeoutException();//抛出,让socket线程终止 } //正常情况下可以获取执行后的值 System.out.println(ct.getResult()); } catch (InterruptedException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } System.out.println("then,we take zhe next step!"); } } class CounterThread extends Thread { // 构造函数,传递一些有用的值给run; public CounterThread(int time) { this.result = time; } private int result; public int getResult() {//在上面获取的结果值 return result; } public void run() { // 执行的任务,如读取客户端数据 try { Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } } }
二、ExecutorService.submit(Thread).get()
此方法经验证是我最喜欢的方法。
package com.ane56.Demo20170904; import java.util.concurrent.*; /** * Created by liyang on 2017/9/4. */ public class TimeoutTest1 { public static void main(String[] args) { final ExecutorService service = Executors.newFixedThreadPool(1); TaskThread taskThread = new TaskThread(); System.out.println("提交任务...begin"); Future<Object> taskFuture = service.submit(taskThread); System.out.println("提交任务...end"); try { Object re = taskFuture.get(6000, TimeUnit.MILLISECONDS);//超时设置,6s System.out.println(re); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { System.out.println("超时 取消任务"); taskFuture.cancel(true); System.out.println("超时 取消任务OK"); } finally { service.shutdown(); } System.out.println("then,we take zhe next step!"); } } class TaskThread implements Callable<Object> { public Object call() throws Exception { String result = "空结果"; try { System.out.println("任务开始...."); Thread.sleep(5000); result = "正确结果"; System.out.println("任务结束...."); } catch (Exception e) { System.out.println("Task is interrupted!"); } return result; } }
相关文章推荐
- 线程进入disk sleep状态,gdb 无法attach
- 。。。。如果由于未处于活动状态导致服务中止了该通道,则安全上下文令牌无效。若要防止服务永久中止闲置会话,请增加服务终结点绑定上的接收超时。
- 关于线程耗尽导致请求超时系统假死的思考
- 线程进入sleep状态,如果加锁,锁不会释放,其他线程无法进入Rum.
- wxpython 拖动界面时进入假死状态(未响应)解决方法
- 关于Java线程状态转换中的wait()被唤醒时进入的状态的疑问
- 线程进入等待状态有几种方式?
- 关于线程耗尽导致请求超时系统假死的思考
- 线程进入等待状态
- 警用综合查询服务处于假死状态
- 线程状态转换调度
- JavaWeb 服务启动时,在后台启动加载一个线程
- 线程的状态----joinable和detached
- 状态线程
- Java线程的生命周期和状态控制_动力节点Java学院整理
- 关于dubbo服务超时的讨论
- 智能手机大战进入下一阶段:整合内容与服务
- Java线程状态图!
- 线程的各个状态(大型公司最喜欢问的多线程问题,面试技巧)