S
shaun82
I've been trying to setup a thread class that runs for a specific
amount of time, then interrupts the original thread using an exception.
So, here is an example:
SomeRandomClass.class
try {
TimerThread e = new TimerThread();
while(1) {
e.resetTime();
e.start();
<some blocking I/O call>
e.end();
}
} catch(RuntimeException ex) {
// exception caught from TimerThread
}
I was trying to make a TimerThread class which basically acted as a
monitor for a blocking I/O call, and would pull that class out of the
I/O call if it "timed out".
TimerThread is a class which extends Thread, and overrides the run()
method.
It's code went something like this:
TimerThread.class
// overridden from Thread class
void run()
{
while(time_left) { ... }
thrown new RuntimeException("TimerFinished");
}
But of course this does not work, because as soon as the
RuntimeException burns through the TimerThread thread, the TimerThread
instance just dies, and the blocking I/O call continues on its merry
way.
So what do you think of this? Is there anyway for me to salvage this
code? Can I somehow pass the RuntimeException to the original caller of
the TimerThread?
Thanks for any help,
Shaun
amount of time, then interrupts the original thread using an exception.
So, here is an example:
SomeRandomClass.class
try {
TimerThread e = new TimerThread();
while(1) {
e.resetTime();
e.start();
<some blocking I/O call>
e.end();
}
} catch(RuntimeException ex) {
// exception caught from TimerThread
}
I was trying to make a TimerThread class which basically acted as a
monitor for a blocking I/O call, and would pull that class out of the
I/O call if it "timed out".
TimerThread is a class which extends Thread, and overrides the run()
method.
It's code went something like this:
TimerThread.class
// overridden from Thread class
void run()
{
while(time_left) { ... }
thrown new RuntimeException("TimerFinished");
}
But of course this does not work, because as soon as the
RuntimeException burns through the TimerThread thread, the TimerThread
instance just dies, and the blocking I/O call continues on its merry
way.
So what do you think of this? Is there anyway for me to salvage this
code? Can I somehow pass the RuntimeException to the original caller of
the TimerThread?
Thanks for any help,
Shaun