Skip to content

Tag: timeout

Java: set timeout on a certain block of code?

Is it possible to force Java to throw an Exception after some block of code runs longer than acceptable? Answer Yes, but its generally a very bad idea to force another thread to interrupt on a random line of code. You would only do this if you intend to shutdown the process. What you can do is to use Thread.i…

HttpURLConnection timeout settings

I want to return false if the URL takes more then 5 seconds to connect – how is this possible using Java? Here is the code I am using to check if the URL is valid Answer HttpURLConnection has a setConnectTimeout method. Just set the timeout to 5000 milliseconds, and then catch java.net.SocketTimeoutExce…