add locking to avoid concurrency issues in JNI