views:

273

answers:

6

I would like to know if it is possible to automatically invoke a Java method when a hardware interrupt is raised.

+2  A: 

In principle yes, but it will require some C code and JNI to tie that to Java. If you are very lucky perhaps already someone has already built a suitable library for the paltform you are interested in.

Bottom line: if it can be done in C you can hook that to Java.

djna
@djna, thanks for the answer. I have been searching for such a java library but in vain.
Sigh..It's Urgent
@Sigh - you'll need a native library that matches you OS and hardware environment. The JNI part is no magic, there are some tutorials on the net.
Andreas_D
+2  A: 

There may be an alternative.

I'm doing something similar: In an application I monitor 4 mice for clicks. Those clicks generate interrupts but I'm happy enough not to deal with them directly from Java.

Under Linux, it turns out there are device files (/dev/input/mouse#) that spew a bunch of characters when something happens with the mouse. I have a Thread for each one with a FileReader blocking on a read. Once characters arrive, the appertaining thread unblocks and I can do whatever processing I like.

So the idea is: If possible, find a way to get a device driver to make the data accessible to you in file/device form, then you can access it from Java using just IO calls from the Java library, with no weird bit-twiddling code and C required in between.

Carl Smotricz
+1  A: 

Here is a paper that handles the same topic. And you may have a look at SWT, I think they're dealing with hardware interrupts aswell, although they may rely on the operating systems API.

Andreas_D
A: 

Hardware interrupts are special - they are frequent and need rapid service. I do not think that doing that in Java will be a good idea since you essentially go into the full JVM including JIT'ing and garbage collection which all in all results in unpredicability.

In my experience hardware interrupts should be done in as low level a language as you can within reason. I.e. C or assembler. Not Java.

Thorbjørn Ravn Andersen
A: 

It's standard on embedded realtime java. go to www.ajile.com, or systrmonx.com and buy an eval board.

Embedded java is not standard on pc's. you can get realtime java on PC hardware, but not the embedded bit.

Tim Williscroft
A: 

Take a look at Swig. The Java implementation has Directors that will allow you to call into Java from C/C++.

I've used this technology to handle interrupts calling into C#, and it worked great. Shouldn't be much different calling Java.

Nick Veys