views:

86

answers:

4

As for as I know, Java threads can communicate using some thread APIs. But I want to know how the Java threads and the OS threads are communicting with each other. For example a Java thread needs to wait for some OS thread finishes its execution and returns some results to this Java thread and it process the same.

A: 

Every Instance of JVM is essentially an OS process.

hakish
An OS process is NOT the same as an OS thread.
Jonas
yup point taken
hakish
A: 

...Why you want such Obscure feature in first place? u use Java for its all pervasive VM , then strike for OS threads...? what for?

i think , u r planning to hack Java VM , right?

rajesh bhide
A: 

Java threads usually but don't necessarily run on native threads and Java concurrency classes could but don't necessarily map onto native equivalents.

If you had to sync between a native thread and a Java thread, you will most likely have to consider writing a JNI method that your Java thread calls. This JNI method would do whatever native synchronization operation it needs to do and then return. Every platform is going to do this differently but I assume this wouldn't be too much of an issue if you need to inspect native threads in the first place.

locka
+5  A: 

Many mix up threads and processes here, the jvm is a process which may spawn more threads. Threads are lighter processes which share memory within their process. A process on the other hand lives in his own address space, which makes the context switch more expensive. You can communicate between different processes via the IPC mechanisms provided by your OS and you can communicate between different threads within the same process due to shared memory and other techniques. What you can't is communicate from ThreadA(ProcessA) to ThreadA(ProcessB) without going through plain old IPC: ThreadA(ProcessA) -> ProcessA -> IPC(OS) -> ProcessB -> ThreadA(ProcessB)).

You can use RMI to communicate between two java processes, if you want to "talk" to native OS processes, you have to go JNI to call the IPC mechanisms your OS of choice provides imo.

Feel free to correct me here :)

Sidenote: You cant see the threads of your JVM with a process manager (as long as your JVM does not map threads to native processes, which would be stupid but possible), you need to use jps and jstack to do that.

atamanroman
Threads are threads. Don't mix up threads and **Light-weight processes**: http://en.wikipedia.org/wiki/Light-weight_process
Jonas
@Jonas yep, i did not want to introduce LWPs here. Changed "lightweight" to "lighter" processes, which is at least less wrong ;)
atamanroman