This is my below code, I am confuse why this thing is happening. In this code getLocationTimeout is a method in which I am passing two things- one is the ip address and second is the timeout in milliseconds. So I will get the timeout exception if the response is not getting back in under 5 milliseconds in this particular case. So when I ran this below code, I am getting few timeout exceptions for few ip address. That means some response is taking longer time (greater than 5 ms) but the most important thing that I am confuse about is if I am getting timeout exceptions (time taken to get the response is greater than 5 ms) then why the program is entering in that if loop in which I am having difference > 5. It should have thrown the timeout exception at previous place. What can be the possible reason for this? It is because of catch block? Any suggestions will be appreciated.
long runs = 10000;
long difference = 0;
while (runs > 0) {
String ipAddress = generateIPAddress();
long start_time = System.nanoTime();
try {
resp = GeoLocationService.getLocationTimeout(ipAddress, 5);
} catch (TimeoutException e) {
System.out.println("Timeout Exception");
}
long end_time = System.nanoTime();
if (resp == null || (resp.getLocation() == null)) {
difference = 0;
} else if (resp.getLocation() != null) {
difference = (end_time - start_time) / 1000000;
}
if (difference> 5) {
System.out.println("Debug");
}
}
OUTPUT NOT EXPECTED
Ok. The problem that I am having is- It should print out Timeout Exception for any response greater than 5 milliseconds. But in some case what’s happening is that it is printing Debug but without printing Timeout Exception and with the help of debugger I found that if the difference is around 6 or 8 then I saw pointer entering that if loop of debug.
Ok, thanks for adding the info – thought I would put this as a different answer, as its a different tack. So from the info you added I think it just looks like the extra time is just from the extra try catch etc instructions its running (I think). However maybe you should look at redoing the code really. The getLocationByIpTimeout call does all the timing you need I think, so then you can just do whatever logic you need in the catch block, i.e. you don’t need all the timing stuff – will that do what you need?