I have some tasks which is limited by time. The task is request to a server and a result is a response. The time is around 10 milliseconds. After time ends I want to receive result immediately(or within 2-5 milliseconds) or null. So I packed my tasks in Callable and naively hoped that Future.get() with timeout will throw exception immediately after timeout reached. But as I see Future.get() with timeout can't provide such precision. In my simple test it has deviation even 12 milliseconds and it's without high load. Now I know that it's normal behavior for Future. But my task is still there and it would be great to have more accurate timeout. I've heard about Java Real Time System, but it seems like very sophisticated tool for me. Is there any simple alternatives to Future.get() and Java Real Time System?
Asked
Active
Viewed 853 times
0
-
1the source of the delay could be that the task does not check the interrupt status often enough. – assylias Apr 21 '14 at 15:11
-
Do you have some source that explains why "it's normal behavior for Future"? I'm interested to know how much variation I can have. – Julien H. - SonarSource Team Jun 06 '14 at 07:14
1 Answers
0
Have you thought about record the timing in the same thread that you are using to call the request with? You can throw exceptions from the underlying thread and when Future.get() is called, those exceptions will be thrown there.
I'm not sure if you're familiar with non-blocking-io, but with this you wouldn't need to block on a read from your stream and continue computation (i.e. recording the time the operation is taking.)
EDIT:
java.nio.* package is a non-blocking io package made available with jse1.7 and later. Here is a link to a basic example using it:
http://www.studytrails.com/java-io/non-blocking-io-multiplexing.jsp

Jake B
- 672
- 1
- 9
- 21
-
I've worked with NIO little bit. With netty. But I don't see how it could help me because I still should wait for response only 10 milliseconds. After that, result doesn't interesting for me. I.e. somehow I've sent a message. What should I do while server computes my request? How can I count exactly 10 milliseconds without a wild while loop. Can you add an example? – Moses Apr 21 '14 at 15:21
-
I don't think you can guarantee that your process will wait for EXACTLY a certain amount of ms, due to cpu scheduling, thread context switching. The best you could do is probably busy wait, which would be spinnging loop like the first answer in the following question: http://stackoverflow.com/questions/9143719/java-alternative-to-thread-sleep – Jake B Apr 21 '14 at 15:36
-