plasma-umass / doppio

Breaks the browser language barrier (includes a plugin-free JVM).
http://plasma-umass.github.io/doppio-demo
MIT License
2.16k stars 175 forks source link

Question: How to launch a new thread from native code? #463

Closed hrj closed 8 years ago

hrj commented 8 years ago

In a native function, I have a Java object and a java.lang.reflect.Method instance. I want to launch this method in a new JVM thread. What's the best way to do this?

In pseudo-code:

myNativeMethod(thread : JVMThread) {
  const javaObj = ....   // the java Object
  const method = ....   // the method to execute

  // How do I launch a new thread and invoke the method on javaObj ?

}

Thanks!

hrj commented 8 years ago

Alternatively, I guess I could create a wrapper java.lang.Thread instance in Java code, whose run method invokes the method, pass the java.lang.Thread instance to native code, and then call thread.start() from native code.

This should avoid using non-standard APIs, but at the cost of a round-trip to Java land.

hrj commented 8 years ago

It looks like there is no official API to launch a new thread from native code. (That makes sense in a way; probably closer to the JVM standard).

So I am instead creating a thread from JVM land in advance, and then sending a wake-up message to it from native. (We have been doing this for other scenarios too, so it's not a big deal).

jvilk commented 8 years ago

Sorry for the delay; I was traveling for awhile.

You're correct; you should not launch a new thread from native code. Instead, you should spawn threads from within Java.