Why is Java Future.get(timeout) & httpclient Not Reliable?

1.  Future.get(timeout)

In Java project we sometime want to run our code during a limit time , we will consider about using Future.get(time, TimeUnit), While this will not always useful , look at the following code

public class FutureTimeoutTest {
@Test
public void test() throws
    ExecutionException,
    InterruptedException {

    ExecutorService exec = Executors.newSingleThreadExecutor();
    final Callable call = new Callable() {
        @Override
        public Object call() throws Exception {
             try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            return 0;
        }
    };
    final Future future = exec.submit(call);
    try {
        future.get(1000, TimeUnit.MILLISECONDS);
        fail("expected TimeoutException");
    } catch (TimeoutException ignore) {
    }
 }
}

Future.get(timeout) does not reliably throw the TimeoutException after the given timeout. Is this normal behavior or can I do something to make this more reliable? This test fails on my machine. However if I sleep for 3000 instead of 2000, it will pass.


There is no reason to expect the test to pass. Given that you submit the task for execution and then wait on its completion, any amount of time could pass before your wait on Future#get() begins, allowing the task plenty of time to exhaust the sleep duration and complete.
In your case, we can assume that the thread running within the Executor gets focus while your main thread running through test() is on hold, despite being in a runnable state. As for the observed difference between stalling the submitted task for two and three seconds, I expect you could find situations where even three seconds is insufficient, depending on what other processes are busy doing on your computer.


You are expecting what is commonly called "real-time" behavior from Java. This cannot be achieved reliably unless you use real-time libraries in a real-time capable Java distribution running on a real-time operating system.

Just to illustrate, the Java thread implementation in modern JVMs like HotSpot relies on the host operating system's native thread scheduler to decide what threads to run when. Unless the thread scheduler is specifically aware of real-time deadlines and stuff, it is likely to take a "whole of system" view when deciding what threads to run when. If the system is loaded, any particular thread may not get scheduled to run for seconds ... or longer ... after the conditions that prevented it running (e.g. waiting for a timer event) have passed.

Then there is the problem that the Java GC may cause all other threads to block.

If you really need real-time behavior from Java, it is available. For example:

However, you should expect to change your applications to use different APIs to give you real-time behavior.


2. httpclient timeout


connectionTimeout: 指的是连接一个url的连接等待时间

测试的时候,将url改为一个不存在的url:“http://my1321321.com”

超时时间3000ms过后,系统报出异常。


soTimeout

指的是连接上一个url,获取response的返回等待时间

测试的时候的连接url为我本地开启的一个url,http://localhost:8080/firstTest.htm?method=test

在我这个测试url里,当访问到这个链接时,线程sleep一段时间,来模拟返回response超时。

将读取response返回超时时间设的时间比那个sleep时间短之后,运行程序给出异常:

java.net.SocketTimeoutException: Read timed out


setConnectionManagerTimeout  取连接的超时设置


原来SocketTimeout设置的超时是指指定时间内服务器端没有反应,而如果两次反应之间的时间间隔小于设置的值是不算超时的。想想也是,下载大文件的超时肯定不能拿整个时间来计算。 

下面是文档中的说明 
'http.socket.timeout': defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). A timeout value of zero is interpreted as an infinite timeout. This parameter expects a value of type java.lang.Integer. If this parameter is not set read operations will not time out (infinite timeout).

 



https://stackoverflow.com/questions/4350941/why-is-java-future-gettimeout-not-reliable

http://wb8206656.iteye.com/blog/1851244

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325712405&siteId=291194637