As a newbie of renjin Java library user, I plan to make use of renjin ScriptEngine to execute computation on multiple threads in parallel. It is obviously fine to initialize an renjin ScriptEngine instance on each thread and then the initialized renjin instance can be used by its belonging thread.
However, as the initialization of one renjin instance takes pretty long time, a question came across my mind: can multiple threads share one enjin ScriptEngine instance and call it in parallel?
You cannot safely evaluate multiple R expressions concurrently using a shared RenjinSession. Technically you could if you verified that none of the R functions you're calling affect session-level state, which includes:
But all of these things are quite common amongst the R functions in the base and stats packages.
If you want to reduce the overhead of initializing new RenjinSessions, consider using ThreadLocal to cache one session per thread, or use something like the Apache Commons Pool library to maintain a pool of initialized sessions that can be quickly obtained by new threads.