Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #23111
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: polling IRQs in a thread's code |
| Date | 2013-03-24 21:54 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <kiol5t$45m$1@dont-email.me> (permalink) |
| References | <thread-20130324233549@ram.dialup.fu-berlin.de> <kio1a2$onu$1@dont-email.me> |
On 3/24/2013 4:14 PM, markspace wrote:
>
> void run()
> {
> if( irq() ) {
> cleanup();
> return;
> }
I posted this, and I didn't much like it when I posted, and now I'm
pretty sure it's rubbish, as they say on the other side of the Atlantic.
SwingWorker got me thinking about better ways to do this.
void run() {
try {
// tons o' stuff
} catch( InterruptedException ex ) {
// clean up
}
}
The trick now is to check for an interrupt often enough. If you're
publishing data periodically with the SwingWorker#publish method, that
might be a good place to check. However you might want to check more
often as well. Either way, throwing an exception seems much easier and
cleaner at the top level than manually breaking your code into discreet
sections.
If you have very complicated clean-up code, you might have to package
the clean-up into discreet chunks so it can be executed by the top level
clean-up code.
class MyTask implements Runnable { // or SwingWorker
private Runnable cleanup;
public void run() {
try {
a();
b();
c();
} catch( InterruptedException ex ) {
if( cleanup != null )
cleanup.run();
}
}
private void a() {
cleanup = new CleanupA();
//... do stuff, then when done, reset cleanup
cleanup = null;
}
private class CleanupA implements Runnable {
public void run() {
// vacuum and dust
}
}
private void b() {
// etc.
}
private void c() {
// etc.
}
}
That seems cleaner to look at, at least for what's written there.
If you do use Runnable, don't overlook this simple transform to get a
more modern version.
Future<X> future = new FutureTask( myTask, x );
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: polling IRQs in a thread's code markspace <markspace@nospam.nospam> - 2013-03-24 16:14 -0700
Re: polling IRQs in a thread's code markspace <markspace@nospam.nospam> - 2013-03-24 21:54 -0700
[OT] Re: polling IRQs in a thread's code Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-03-25 08:43 -0400
Re: [OT] Re: polling IRQs in a thread's code markspace <markspace@nospam.nospam> - 2013-03-25 13:27 -0700
Re: [OT] Re: polling IRQs in a thread's code Lew <lewbloch@gmail.com> - 2013-03-25 13:44 -0700
Re: [OT] Re: polling IRQs in a thread's code Gene Wirchenko <genew@telus.net> - 2013-03-26 11:41 -0700
Re: [OT] Re: polling IRQs in a thread's code paul.cager@gmail.com - 2013-03-26 12:03 -0700
Re: polling IRQs in a thread's code Robert Klemme <shortcutter@googlemail.com> - 2013-03-25 23:15 +0100
[OT] Re: polling IRQs in a thread's code Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-03-25 19:58 -0400
Re: [OT] Re: polling IRQs in a thread's code Robert Klemme <shortcutter@googlemail.com> - 2013-03-26 08:03 +0100
Re: [OT] Re: polling IRQs in a thread's code Robert Klemme <shortcutter@googlemail.com> - 2013-03-26 08:07 +0100
csiph-web