You can set the timeout like this:
long connectTimeout = 3000;
URL global_url = new URL(urlPath);
URLConnection con = global_url.openConnection();
con.setConnectTimeout(connectTimeout);
where connectTimeout you can set as in milliseconds. As you need 3s timeout so set it as 3000.