pub struct Executor { /* private fields */ }
Expand description
Thread attachment manager. It allows to execute closures in attached threads with automatic
local references management done with with_local_frame
. It combines the performance benefits
of permanent attaches whilst removing the risk of local references leaks if used consistently.
Although all locals are freed on closure exit, it might be needed to manually free locals inside the closure if an unbounded number of them is created (e.g., in a loop). See “Local Reference Management” for details.
Threads using the Executor are attached on the first invocation as daemons, hence they do not block JVM exit. Finished threads detach automatically.
Example
let exec = Executor::new(jvm);
let val: jint = exec.with_attached(|env| {
let x = JValue::from(-10);
env.call_static_method("java/lang/Math", "abs", "(I)I", &[x])?.i()
})?;
assert_eq!(val, 10);
Implementations§
source§impl Executor
impl Executor
sourcepub fn with_attached_capacity<F, T, E>(
&self,
capacity: i32,
f: F
) -> Result<T, E>where
F: FnOnce(&mut JNIEnv<'_>) -> Result<T, E>,
E: From<Error>,
pub fn with_attached_capacity<F, T, E>( &self, capacity: i32, f: F ) -> Result<T, E>where F: FnOnce(&mut JNIEnv<'_>) -> Result<T, E>, E: From<Error>,
Executes a provided closure, making sure that the current thread is attached to the JVM. Additionally ensures that local object references are freed after call.
Allocates a local frame with the specified capacity.
sourcepub fn with_attached<F, T, E>(&self, f: F) -> Result<T, E>where
F: FnOnce(&mut JNIEnv<'_>) -> Result<T, E>,
E: From<Error>,
pub fn with_attached<F, T, E>(&self, f: F) -> Result<T, E>where F: FnOnce(&mut JNIEnv<'_>) -> Result<T, E>, E: From<Error>,
Executes a provided closure, making sure that the current thread is attached to the JVM. Additionally ensures that local object references are freed after call.
Allocates a local frame with the default capacity.