javastalled

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


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?


Solution

  • I think you can use this:

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