How to throw an exception after a period of time if a certain line is not reached?

1k Views Asked by At

I have a problem when tryig to connect to a server using java.

Lets say that on line:

server.connect()

the program just stalls, no exceptions are thrown.

What I would like to do is throw an exception after 5 seconds if it is stalled. But since I can't get to the next line how would this be possible?

something like

if(after 5 sec server.connect is stalled){
throw an exception
 } 

And would that even solve the problem?

1

There are 1 best solutions below

6
On BEST ANSWER

I think you can use this:

connection.setConnectTimeout(5000);//This value is in Mili seconds