Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #23107
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: polling IRQs in a thread's code |
| Date | 2013-03-24 16:14 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <kio1a2$onu$1@dont-email.me> (permalink) |
| References | <thread-20130324233549@ram.dialup.fu-berlin.de> |
On 3/24/2013 3:45 PM, Stefan Ram wrote:
>
> Is this the usual way to proceed? Or can I simplifiy this somehow?
>
Yes and no. First, I wish you would not use IRQ as your method name.
Interrupt ReQuest is a hardware thing, and we don't have access to those
from within the Java API.
Second, I'd use interrupt(). It's the usual way of asking that a thread
stop. Many routines in the Java API already check for the interrupt
flag for you, which releaves you of some of the burden for testing.
Likewise, wait() and similar fuctions also test for interrupts even when
the thread isn't running, something you would be hard pressed to do.
Third, your code could be structured a little better.
void run()
{
if( irq() ) {
cleanup();
return;
}
firstStep();
if( irq() ) {
cleanup1();
return;
}
secondStep();
if( irq() ) {
cleanup3();
return;
}
...
I hope you see the pattern. Don't use cascading if-else. Just return
from the run() method, it's cleaner. (Several of your braces appeared
to be unbalanced when I corrected the code.) There's probably better
ways of handling the clean-up code, but without more knowledge of what
you are doing, it's hard to say.
Lastly, look at java.util.concurrent.Executor and other frameworks in
the concurrent package. They provide a lot of support for mulithreading.
P. S. Get Java Concurrency In Practice. It's just about required for
working with concurrency in Java.
Back to comp.lang.java.programmer | Previous | Next — 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