-----Original Message-----
From: Nathan Meyers [mailto:[EMAIL PROTECTED]]
Sent: Thursday, November 18, 1999 3:32 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: Thread in the Linux's JVM


On Thu, Nov 18, 1999 at 11:59:04AM -0600, [EMAIL PROTECTED] wrote:
> This sounds to me like Java and C/C++ native code can talk to each other
> only if they (java and native code) are in the same thread.  Is it right,
or
> in the same process?

What do you mean "talk to each other"?

[Lee]
{
    By "talking to each other", I meant that java calls native method; 
    and native code calls back to java.
}

The JVM is a multi-threaded native application. Most of the time it's
running native code that implements an interpreter, but some of the time
it's running native code that doesn't happen to be in the interpreter
(such as JNI methods or JIT-compiled code). Nothing magic (thread-wise)
happens when it moves between the interpreter and other native code -
no new thread is created - it just happens to be running code outside of
the interpreter.  The interpreter jumps to compiled code when it runs
a JNI method, and compiled code can call interpreted code through some
of the functions in the JNINativeInterface structure.

So maybe you can clarify the term "talk to each other"?  When method foo()
calls method bar(), is foo() "talking to" bar()? Method bar() isn't even
running until foo() calls it... once that happens, yes, bar() is running in
the same thread (regardless of language).

[Lee]
{
    This is a great comment!  I didn't pay attention to the 
    simple but important fact - "The JVM is a multi-threaded 
    native application". Can we say that when java calls a native 
    method, what really happens is JVM, the native application, 
    loads a shared lib into its (JVM's) memory space and invokes 
    one of the shared lib's methods.  Therefore, no new thread is 
    created at the time when java calls a native method.  Is it 
    right or I miss something again here?
    
    If it's true, then why you say "When Java creates a native 
    thread" below?  Is a new thread really created when java calls
    a native method, and how?
}
 

> Suppose C/C++ native code creates another thread within the shared lib,
can
> the newly-created thread interact with (call back to) java code?  The
> newly-created thread should be in the same process as its parent thread
and
> java code, right?

When Java creates a native thread, it also creates additional information
for use by the JVM. If your own native code creates a thread, it needs
to "attach" it to the JVM so the JVM knows about it. You'll find some
discussion and sample code at:

 
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/j
niref.html

[Lee]
{
    Yes.  Thanks.
}

The important thing is that both the JVM and your native code use the
same threading mechanism. Since you're using pthread_create() to create
threads, you need to run a version of the JVM (native) that uses the
same system.  AFAIK, there's no way for JNI code to create new threads
in a green thread environment.

[Lee]
{
    They uses the same threading mechanism or the same thread?
}

At any rate, all threads (native or green) are running "in the same
process". With native threads, however, different threads have their
own PIDs and entries in the process table, which results in confusing
"ps" results. (Which is why I put "in the same process" in quotes - if
you define a "process" as something with its own PID, then the threads
are processes... but they *are* lightweight processes!)

[Lee]
{
    http://pauillac.inria.fr/~xleroy/linuxthreads/README says that
    threads not sharing parent's pid is a known bug (see "KNOWN BUGS 
    AND LIMITATIONS" section).  Is it right?

    Can you see a new process besides "java" process after java calls 
    a native method?  I made a simple jni code, run it and couldn't 
    find extra process besides "java" by using "ps -a".  The native 
    method is a infinity loop, so I have time to check ps.
}
Nathan


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


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

Reply via email to