timout on line of code

I

Ike

If I have a URLConnection uc, I would like to permit the following line of
code to execute in say, ten seconds:

InputStream inputStream = uc.getInputStream();

If something is wrong on the server, this line of code hangs, and I'd like
to catch it after, say ten seconds, and have it segue to another line of
code. How do you do that? -Ike
 
S

Stefan Schulz

If I have a URLConnection uc, I would like to permit the following
line of code to execute in say, ten seconds:

InputStream inputStream = uc.getInputStream();

If something is wrong on the server, this line of code hangs, and I'd
like to catch it after, say ten seconds, and have it segue to another
line of code. How do you do that? -Ike

Normally, the connection attempt will time out on it's own. You could
put it off to another thread, and interrupt the thread if it has not
finished after some set time limit (I'm not sure this will work, but
it is the only idea i have right now), but you can not directly specify
such a timeout.
 
K

Knute Johnson

Ike said:
If I have a URLConnection uc, I would like to permit the following line of
code to execute in say, ten seconds:

InputStream inputStream = uc.getInputStream();

If something is wrong on the server, this line of code hangs, and I'd like
to catch it after, say ten seconds, and have it segue to another line of
code. How do you do that? -Ike

Just what would make that line of code hang?
 
I

Ike

Just what would make that line of code hang?

A google search reveals numerous times others have, in the past, had
inexplicable, eternal hanging on:

URLConnection.getInputStream();



Under all different JVMs. Has anyone ever figured out the resolution to
this? We havent changed anything, just, suddenly, faced with eternal hanging
on this particular line. -Ike
 
K

Knute Johnson

Ike said:
A google search reveals numerous times others have, in the past, had
inexplicable, eternal hanging on:

URLConnection.getInputStream();



Under all different JVMs. Has anyone ever figured out the resolution to
this? We havent changed anything, just, suddenly, faced with eternal hanging
on this particular line. -Ike

1.5 has a setReadTimeout() that will throw a SocketTimeoutException if
the read timeout is exceeded. If you can't use 1.5, you could use an
alligator but it would have to close your program. There was a similar
problem with a feature of JavaSound before 1.5 came out and it could
hang. Use a Timer to call System.exit() if it waits too long.
 
I

Ike

Knute,

Can you think of a way in Java such that, using a timer, it works almost as
a "goto" statement if it times out? -Ike
 
K

Knute Johnson

Ike said:
Knute,

Can you think of a way in Java such that, using a timer, it works almost as
a "goto" statement if it times out? -Ike

Ike wrote:


hanging


1.5 has a setReadTimeout() that will throw a SocketTimeoutException if
the read timeout is exceeded. If you can't use 1.5, you could use an
alligator but it would have to close your program. There was a similar
problem with a feature of JavaSound before 1.5 came out and it could
hang. Use a Timer to call System.exit() if it waits too long.

Here's the problem. If you can't use 1.5 and get your call to throw an
exception you can't bypass the hung instruction. You can use a timer
but it is running in a different thread so you can't interrupt the
thread with the hung instruction. Calling System.exit() works however
undesirable that is. You could try using some other form of socket to
connect instead of URLConnection and then closing the socket will cause
an exception to be thrown.

My only other suggestion is to change the architecture of your program
and create a connection manager that spawns the connections and if they
don't report back in time, create another one. That should be
satisfactory if only a small percentage of your connections actually hang.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top