From d71bd8ee72d2cc497c266bd6aee30d62f4f104e4 Mon Sep 17 00:00:00 2001 From: miro Date: Sun, 1 Sep 2024 00:46:02 +0800 Subject: [PATCH] refactor(bindings/java): replace `num_cpus` with `std::thread::available_parallelism` (#5080) refactor(bindings/java): replace num_cpus with std available_parallelism --- bindings/java/Cargo.toml | 1 - bindings/java/src/executor.rs | 9 ++++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bindings/java/Cargo.toml b/bindings/java/Cargo.toml index ba2b8848e2c..e9b27e5683a 100644 --- a/bindings/java/Cargo.toml +++ b/bindings/java/Cargo.toml @@ -157,7 +157,6 @@ services-yandex-disk = ["opendal/services-yandex-disk"] [dependencies] anyhow = "1.0.71" jni = "0.21.1" -num_cpus = "1.15.0" once_cell = "1.19.0" # this crate won't be published, we always use the local version opendal = { version = ">=0", path = "../../core", features = [ diff --git a/bindings/java/src/executor.rs b/bindings/java/src/executor.rs index 4c423b06bed..1ce861c3030 100644 --- a/bindings/java/src/executor.rs +++ b/bindings/java/src/executor.rs @@ -18,6 +18,8 @@ use std::cell::RefCell; use std::ffi::c_void; use std::future::Future; +use std::num::NonZeroUsize; +use std::thread::available_parallelism; use jni::objects::JClass; use jni::objects::JObject; @@ -152,5 +154,10 @@ pub(crate) fn executor_or_default<'a>( /// /// This function could be only when the lib is loaded. unsafe fn default_executor<'a>(env: &mut JNIEnv<'a>) -> Result<&'a Executor> { - RUNTIME.get_or_try_init(|| make_tokio_executor(env, num_cpus::get())) + RUNTIME.get_or_try_init(|| { + make_tokio_executor( + env, + available_parallelism().map(NonZeroUsize::get).unwrap_or(1), + ) + }) }