JNI Multithread Question in Linux

2004-01-07 Thread Kok Choon Kiat








Dear friends,

 

I have successfully integrated native (C++) codes which
employ threading operation with a simple java program. The objective of the
program is to spawn thread (using POSIX pthread_create) that prints 10
“Hello World” in approx. 10 seconds. 

 

My Software environment

JBuilder9 (jdk1.4) in Linux 7.2

 

I have created a shared library

A shared library libhelloworld.so
(g++ using native CJNI_Layer.cpp & CNative.cpp) is created.

 

Running the application

java CJNI_Layer

 

Java Program Output

Native thread prints “Hello World” 10 times (in
approx. 10 seconds) before returning to Java
program (CJNI_Layer.java)

 

Seems that I have achieved what I wanted. However, a closer
look at the java program depicts that the thread should terminate and exit way before the Native Method ThreadFunction()
completes its execution of printing 10 “Hello World”. This is
because the java program (main process) should have exited before the native
method completes execution.

 

It appears to me that the native thread has seized the entire flow of control from java
program and it is not running independently. Why is that so? How can I make the
native thread independent and not seize the flow of control from the java
program? I would really appreciate if you can my answer my questions. Thank you
very much.

 

JJ

 

--- CJNI_Layer.java ---

class CJNI_Layer

{

   
static

{

   
try

   
{

   
System.loadlibrary(“helloworld”);

   
}

   
catch (UnsatisfiedLinkError ule)

   
{

   
System.out.println(ule);

   
}

   
}

 

   
private native int SpawnThread();

 

   
public static void main(String args [])

   
{

   
CJNI_Layer c = new CJNI_Layer();

 

   
c.SpawnThread();

   
}

} 

 

--- CJNI_Layer.cpp --- 

#include CJNI_Layer.h    // generated by javah

#include “CNative.h”
  // defines structure of Class CNative

 

JNIEXPORT jint JNICALL Java_CJNI_Layer_ SpawnThread(JNIEnv
*, jobject)

{

   
CNative native_class;
// an array of CNative

   
native_class.SpawnThread();   

   
return 0;

}

 

--- CNative.cpp --- native implementation
in C++

#include 

#include 

#include “CNative.h”

 

void *thread_func(void
*p)    // this
function is declared as a friend
to CNative in CNative.h

{

   
CNative *pCNative = reinterpret_cast(p);

   
If(pCNative)

   
pCNative->ThreadFunction();

   
return 0;

}

 

void CNative::ThreadFunction()

{

   
for (int i = 0; i < 10; i++){

   
    printf(“\nHello
World”);

   
    sleep(1);

   
}

   
return;

}

 

int CNative::SpawnThread()

{

   
int res = pthread_create(&thread, NULL, thread_func,
this);   // protected class member pthread_t thread

   
return res;

}

 








Re: JNI Multithread Question in Linux

2004-01-07 Thread Paul Mclachlan
Kok Choon Kiat wrote:

It appears to me that the native thread has *seized the entire flow of 
control* from java program and it is not running independently. Why is 
that so? How can I make the native thread independent and not seize 
the flow of control from the java program? I would really appreciate 
if you can my answer my questions. Thank you very much.

 

Add a "System.exit( 0 );" to the end of your main() method.

When main() ends, all that means is that that thread has finished.  If 
your process has other (non-daemon) threads running, it will continue to 
run until they finish, also.  Unless you hard-terminate the process.

If you're trying to prove a point, you could add a random sleep in 
before calling System.exit(0), then you'd see nondeterminism at work and 
could be happy that it was doing what you wanted.

On the other hand, if what you're after (and I can't tell from your 
email, sorry), is that the "Hello World"'s keep printing even after the 
java process has exited, you need a Runtime.exec() or fork(), not a 
pthread_create().  I suspect you know this, but thought I'd throw it in 
just in case.

HTH

- Paul

--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]


Re: JNI Multithread Question in Linux

2004-01-07 Thread Neal Sanche
On January 7, 2004 10:37 pm, Paul Mclachlan wrote:
> Kok Choon Kiat wrote:
> > It appears to me that the native thread has *seized the entire
> > flow of control* from java program and it is not running
> > independently. Why is that so? How can I make the native thread
> > independent and not seize the flow of control from the java
> > program? I would really appreciate if you can my answer my
> > questions. Thank you very much.
>
> Add a "System.exit( 0 );" to the end of your main() method.
>
> When main() ends, all that means is that that thread has finished. 
> If your process has other (non-daemon) threads running, it will
> continue to run until they finish, also.  Unless you hard-terminate
> the process.
>
> If you're trying to prove a point, you could add a random sleep in
> before calling System.exit(0), then you'd see nondeterminism at
> work and could be happy that it was doing what you wanted.
>
> On the other hand, if what you're after (and I can't tell from your
> email, sorry), is that the "Hello World"'s keep printing even after
> the java process has exited, you need a Runtime.exec() or fork(),
> not a pthread_create().  I suspect you know this, but thought I'd
> throw it in just in case.

I agree with what Paul said above and would also add that I recently 
read that one of the more recently JDK 1.4 versions also added the 
ability for Native threads to be 'daemon' threads. Which means the 
JVM will exit even if they are running. What JJ is seeing is a 
non-daemon native thread keeping his JVM from exiting, and I think he 
was surprised by that.

Oh well, fun stuff to think about, but not exactly anything concerning 
Linux, other than the fact that it was the platform on which the 
behaviour was observed.

Cheers.

-Neal


--
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]