These days have been busy writing a Java program for data synchronization, and I have encountered a big problem. The problem is this: My data synchronization program is to run four threads at the same time, in one of the threads, to be a MD5 file check job. The verification of this MD5 is to be implemented by calling a DLL, this DLL (name is MD5Operation.dll) is an MD5 operation for the file content, the purpose is to use the server side and the client's file check.
The source code of class MD5Tool is as follows:
Package com.hode.util;
Public class md5tool {public static native string HMD5; static {system.loadLibrary ("md5opration");}}
I declare a static local method HMD5 so that I can call directly in the program to call, after my simple test, the program runs normally. The test code is as follows:
Package com.hode.util
public class TestMD5 {public static void main (String [] args) {String filePathName = "C: // my music // heal the world.mp3"; String fileMD5Str = MD5Tool.hMD5 (filePathName); System.out.println ( Filemd5Str = " filemd5str);}}
But when I get it getting it to the program that synchronizes it, when the program runs to MD5Tool.hmd5 (filepathname), inexplicably termination, and will not throw any exceptions, this is really not understanding . I have been looking for a half-day reason, I can't find any questions. Because after all, I will call the MD5 code, then turn it out to toss a few times or that look. I really can't make a bit uncertain. At this time, Neil takes a "Java Thread". He found a page about invigo the Native method, the book says that in multithreaded environments, when calling the native method, you must declare It is synchronized static, otherwise there will be problems and cause the thread to terminate. Hey! It is a bit mean, I have rushed the code to change the code, my code became:
Package com.hode.util;
Public class md5tool {public synchronized static native string hmd5; static {system.loadLibrary ("md5otation");}}
Run again, the result is disappointing, the program will still terminate! Where is the problem? ? I am a bit discouraged ...
Since the "Java Thread" has been explained, the Native method can be invoked in a multi-threaded environment may have a problem, then if there is a problem when loading the DLL in the subclock? I don't know, I am scared, I put the DLL load in the main thread, then call in the sub-thread, the code is simply as follows:
Package com.hode.sync.thread; import com.hode.util.md5tool;
Public class mainthread extends thread {private md5tool mymd5tool = new MD5TOOL (); // Load DLL .... .... .... ....
Public static void main (String [] args) {.... .... ....
Subthread1 T1 = new subthread1 (); Subthread1.run ();
.... .... ....}}
Public class subthread1 extends thread {public void run () {.... .... ....
String filepathname = "c: // my music ///HEAL THE World.mp3"; string filemd5str = md5tool.hmd5 (filepathname); // Call HMD5 method in the subclicer System.out.println ("FileMD5Str =" filemd5str );
.... .... ....}}
Successful! Oh, it turns out that when you call the native method, you should pay attention to the following: (1) When you run at the same time, it is best to declare the native method into synchronized static. (2) DLL load To do in the main thread, but can be called in the subclock.