Efficiency and convenience
How to use the local method without restriction availability
Summary:
When the Java class uses the local method, even if the current running system cannot locate or cannot correctly use a dynamic connection library, you can also maintain the convenience of this method by providing rely.
Author: Nick Efford
Java has a lot of happiness, but sometimes it looks relatively slow. The area I am interested is image processing, and this aspect mentioned is prone to the situation. Concentration to a little in the image processing, the frequent array access, high intensity calculation significantly affects the performance of Java code. On my machine, a 512 * 512 image 3 * 3 rotates, the speed of execution of Java is 40 times slower than C . JIT compiler and hardware acceleration should ultimately improve this problem, but there should be another solution, which is to use local methods.
Of course, the main problem with the use of local methods is that your Java program is not enough, and the transplantability is deteriorated. In the effect, you lost the current network centering computing environment to make Java interesting and important real features. Under some circumstances, the best choice of Java may be simply forgot, using a language to develop your application is best to match your problem domain. If you need to use Java, it can use the relying method to use the local method without reducing convenience.
The basic method is to provide an equivalent non-local approach to the defense system without positioning, or the dynamic connection library is not available.
Your classes require some means to mark whether the local method is available, this can be implemented by using the Boolean type member variable. Set the true and false value of the flag bit according to the actual loading dynamic connection library.
Public class condom {
Private static boolean _native;
STATIC {
Try {
System.err.Println ("Convolver: Loading Dynamic Library ...");
System.loadLibrary ("convolver");
_native = true;
}
Catch (unsatisfiedlinker e) {
System.err.Println ("Convolver: no library found -"
"Using Fallback Methods ...");
_native = false;
} Catch (exception e) {
System.err.println ("Convolver: Cannot Load Library - "
"Using Fallback Methods ...");
_native = false;
}
}
...
}
In the above example, _native is used to mark whether the local method is available. If the System.LoadLibrary method is successful, it will be set to True. If the specified dynamic connection library cannot be found, or LD_Library_Path is not set correctly, the unsatisfiedlinkerror exception will be thrown. We capture this exception, then set _native to false (simultaneously output a warning message, notifying the spare method).
In the second CATCH block of the previous example, it may be thrown because of other reasons, such as the authority check of the Applet program, so it is also necessary to capture this exception, then set _native to false (simultaneously output a warning message, notify the spare method Will be used).
Attention The user should not call the local method or alternate method, and the class should make yourself to decide what time loaded, decide whether to use local methods or alternate methods. Therefore, the local method and the standby method should be set to private. For example, the following code: public void convolve () {
IF (_native)
NativeConvolve ();
Else
FallbackConvolve ();
}
Private native void nativeconvolve ();
Private void FallbackConvolve () {
...
}
In this example, a rotation operation can be executed by the local method NativeConvolve or alternate method FallbackConvolve, and the specific selection of the two calls is independent of the caller, and only simply call the convolve method.
For the latest version of Sun's latest version, please see http://java.sun.com/books/series/tutorial/index.html
About the Author:
Nick Efford is a lecturer at the computer college at U.K. Leeds University. His main research is the intelligent segmentation and 2D, 3D model of medical images, and currently teach image processing courses.