并发包之Future:代码级控制超时时间
来源:互联网 发布:nlp领域算法 编辑:程序博客网 时间:2024/06/14 14:56
先谢Doug Lea。
使用场景:
最近在做webservice调用的时候,发现一个问题,对方的webservice接口很不稳定,所以在获取的数据时候经常要等待很久才能把数据全部拉回来,甚至有时候直接就抛异常了,这种阻塞很耗费时间,性能很低。针对这种情况,我需要将一定时间内没有返回结果的调用KILL掉,于是用到了并发包Future,实现并在生产环境运行良好。
============================================================
2015-07-13修改备注:
1、之前的代码存在executor没有关闭的异常;
2、对新来的朋友提个建议,频繁创建single线程池不是一个好建议。我之前的场景并发很低,看不出问题。随着现在接触的业务并发变得很大,根本不会这么写了,因为频繁的创建关闭线程会浪费很多资源。并发很小的情况,并且必须要控制超时时间的情况下可以试试。
很抱歉,本人阅历有限,希望没对读者造成影响。
============================================================
测试代码如下,自行体会:
package com.array7.concurrent;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;public class FutureTest {public static void main(String[] args) {String result = ControlTimeOut.call(new CallableImpl("OK"));System.out.println(result);result = ControlTimeOut.call(new CallableImpl("NO OK!"));System.out.println("result=" + result);}}class ControlTimeOut {@SuppressWarnings("unchecked")public static <T> T call(Callable<T> callable) {ExecutorService executor = Executors.newCachedThreadPool();Future<T> future = executor.submit(callable);try {T t = future.get(3000, TimeUnit.MILLISECONDS);executor.shutdwon();return t;} catch (InterruptedException e) {System.out.println("InterruptedException");} catch (ExecutionException e) {System.out.println("ExecutionException");} catch (TimeoutException e) {// TODO: coding here...System.out.println("TimeoutException");}return null;}}class CallableImpl implements Callable<String> {private static final String CORRECT_KEY = "OK";private String key = "";public CallableImpl(String key) {this.key = key;}public String call() {// TODO:真正的业务逻辑if (CORRECT_KEY.equals(this.getKey())) {return "SUCCESS";} else {try {Thread.sleep(5000);// 阻塞。设置5秒超时,为了Future抛出TimeoutException} catch (InterruptedException e) {e.printStackTrace();}return "FAIL";}}public String getKey() {return key;}public void setKey(String key) {this.key = key;}}
0 0
- 并发包之Future:代码级控制超时时间
- Java 多线程设置线程超时时间之 Callable接口和Future接口 线程超时控制
- Java 多线程设置线程超时时间之 Callable接口和Future接口 超时控制
- java 1.7控制代码超时时间
- java并发包:future模式
- Transactional超时时间控制
- Transactional超时时间控制
- Java 多线程设置线程超时时间之 Callable接口和Future接口
- Java 多线程设置线程超时时间之 Callable接口和Future接口
- 并发程序设计之Future模式
- Java并发之Callable、Future
- java并发包学习系列:future模式
- java并发包学习系列:future模式
- Transactional超时时间timeout控制
- DSS 代码分析【Reliable UDP之超时时间计算】
- Transactional超时时间控制与mysql事务超时时间
- java 并发之Callable和Future
- Java并发编程之Callable,Future,FutureTask
- POJ 2431 Expedition(树)
- java JSP指令
- 【hadoop】学习笔记-安装配置
- ajax 详解(未完成)
- http://bbs.tiexue.net/post_7657028_1.html http://bbs.tiexue.net/post_7657237_1.html http://bbs.tiex
- 并发包之Future:代码级控制超时时间
- Multi-Thread 3---Use thread Pool
- http://bbs.tiexue.net/post_7657451_1.html http://bbs.tiexue.net/post_7657472_1.html http://bbs.tiexu
- 实现:上传图片后再把图片显示到页面上去
- "ios模拟器未能安装此应用程序"
- Mac电脑:喇叭无声,耳机插口处亮红灯-续
- Parity game
- 那一月、那一年、那一世
- Multi-Thread 4--- Lock & Conditions