• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple utility to implement a timeout

 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want a simple utility that helps me implement a timeout in Java. It should be able to throw some kind of Exception.

I tried using Timer and Timertask, also tried to use concept of two threads but unable to really use them, since the main thread must be in a wait(), sleep() state before it can receive the InterruptedException.

Here is my scenario:
[edit]Newlines to keep to width of screen[/edit]
 
Campbell Ritchie
Marshal
Posts: 52531
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't write lines so long in code tags; it is difficult to read.

Not a "beginner's" question; I think this would sit better on the "threads" forum. Moving.
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Shindikar wrote:Hi,

I want a simple utility that helps me implement a timeout in Java. It should be able to throw some kind of Exception.

I tried using Timer and Timertask, also tried to use concept of two threads but unable to really use them, since the main thread must be in a wait(), sleep() state before it can receive the InterruptedException.

Here is my scenario:
[edit]Newlines to keep to width of screen[/edit]


Have a look here.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey rodrigo, thanks for the link !!!

i have a few questions based on it though...

i am a complete novice as far as exposure to corba is concerned. So, can we use Corba Exceptions as if they were Java Exceptions ?

If the answer is yes, how do we set time-to-live parameter on whose expiry, the TIMEOUT exception is called.
 
Henry Wong
author
Sheriff
Posts: 22524
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using Timer and Timertask, also tried to use concept of two threads but unable to really use them, since the main thread must be in a wait(), sleep() state before it can receive the InterruptedException.



In general, the easiest way to implement a timeout is to do it cooperatively. You can use a timer task that will set a flag -- that a timeout occured. And the main thread can then check the flag, and return appropriately. The timer task can interrupt the main thread, and it can also close the main thread resources (such as files, etc.). But it will be done to generate an Exception, so that the main thread can catch it, check the flag, and terminate appropriately.

There really isn't a magical way to do it that will work in all cases. It has to be cooperative, as you don't want the main thread's resources to be corrupted after a timeout.

Henry
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

If you have had a look at my code snippet, when the executeSomething() is being executed, the invocation can cause this executing thread(main thread) to hang! In that case, there is no way in which I can check the flag to decide what must be done.

Moreover, the main thread will be interrupted using InterruptedException only if it is in wait, sleep states. If it is hangs in the way that I mention, I doubt if the Timertask can be of any help.

Your views would be appreciated!
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it will be done to generate an Exception, so that the main thread can catch it


Precisely. If you can tell me how can I do this using TimerTask and the currently executing thread, that is exactly what I want.

 
Henry Wong
author
Sheriff
Posts: 22524
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Shindikar wrote:
If you have had a look at my code snippet, when the executeSomething() is being executed, the invocation can cause this executing thread(main thread) to hang! In that case, there is no way in which I can check the flag to decide what must be done.

Moreover, the main thread will be interrupted using InterruptedException only if it is in wait, sleep states. If it is hangs in the way that I mention, I doubt if the Timertask can be of any help.


Unfortunately, for it to work in every case, it has to be cooperative -- meaning you need to have executeSomething() handle it. For if it is sleeping or waiting, you need to interrupt it. If it is doing a long IO block, you need to close the stream. For everything else, you need the method to check the flag.... and even with all of this, you need executeSomething to exit gracefully, so that it may be called again, and not have corrupted data.

And the purpose of the timer is to trigger the process -- to know when to start.


If you want to be able to cause a timeout (with any method, even something that was originally code to not handle a timeout), then sorry... I don't think that it can be done.

Henry
 
Henry Wong
author
Sheriff
Posts: 22524
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to be able to cause a timeout (with any method, even something that was originally code to not handle a timeout), then sorry... I don't think that it can be done.


BTW, the statement is not completely true.... the thread's stop method was originally meant to do this...

Basically, you can cause any thread to throw any exception with a call to stop(Throwable). The main thread can then simply catch the throwable, by wrapping the executeSomething(), in a try catch. And the timer task can simply call stop(), with the correct throwable on the main thread.

This method is deprecated. And it is deprecated for the reason why timeout must be cooperative. As it is not possible to do this without corrupting data. In fact, sometime when this is used, it may even corrupt internal data used by the JVM.

Henry
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!

Suppose if I can guarantee that I will not be corrupting any kind of data, is it advisable to go ahead and still use stop() method ?

The point here is, I understand why it needs to be cooperative; but I intend to create a timeout which can be used for multiple reasons like connection timeouts, command timeouts et all.
 
Henry Wong
author
Sheriff
Posts: 22524
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad Shindikar wrote:
Suppose if I can guarantee that I will not be corrupting any kind of data, is it advisable to go ahead and still use stop() method ?


How are you going to "guarantee" that no data will be corrupted, when the JVM can't make this guarrantee? Keep in mind, that when I say corrupted data, I don't mean just user data, it includes internal JVM data as well.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!