Can JNI help Java use long running, multi-threaded native code?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Fred_Clewis
Posted On:   Monday, December 1, 2003 01:16 PM

Sorry for the JNI newbie question, I am trying to figure out if JNI can help in my situation or if it is necessary to write the entire application in C++. I have a C/C++ AIX API for X.25 communication, AIXLink. I would happily chunk this API if there is a Java counterpart. On the C side, it is necessary to start many long running threads to handle the multiple X.25 channels. Once a channel connection is established to a stream, you would want to leave it up across JNI calls. Also, the C part needs to get responses and unsolicited requests back to the Java part. Obviously, the threads would have to be created and managed on the C side. All of the examples I see tend to be Java calls native, native does job and returns   More>>

Sorry for the JNI newbie question,


I am trying to figure out if JNI can help in my situation or if it is necessary to write the entire application in C++. I have a C/C++ AIX API for X.25 communication, AIXLink. I would happily chunk this API if there is a Java counterpart. On the C side, it is necessary to start many long running threads to handle the multiple X.25 channels. Once a channel connection is established to a stream, you would want to leave it up across JNI calls. Also, the C part needs to get responses and unsolicited requests back to the Java part. Obviously, the threads would have to be created and managed on the C side.


All of the examples I see tend to be Java calls native, native does job and returns to Java, the native part ceases execution.


Can JNI help with this? Is this what callbacks are for? Or is this a case where execution should start on the native side and it does JNI to Java?


thanks for any ideas,

   <<Less

Re: Can JNI help Java use long running, multi-threaded native code?

Posted By:   lang_qiu  
Posted On:   Monday, December 1, 2003 08:25 PM

Yes, JNI can help you. You can attach your thread to JVM.

And actually you must do it this way when you try to call java codes from within c/c++ sides.Otherwise, the result is unexpected.

First, when you create your native thread, you need to get JavaVM using env->GetJavaVM( &m_vm );

Then, in your native thread's main(run) method, attach it to jvm using m_vm->AttachCurrentThread(...)

Before your native main method exit, using m_vm->DetachCurrentThread() to Detach the thread from VM

About | Sitemap | Contact