Singlethread vs threadsafe Context

What is really the difference between “singlethread” and “threadsafe”
Context Instance Type?

Does this affect the thread safety of the ScriptEngine’s eval() method
or invokeFunction() or both?

Unrelated question. Does invokeFunction actually parses the script
again? Or does parsing only occur once during the eval() call?

Thanks
Budyanto

Hi Budyanto,

On Mon, Mar 15, 2010 at 10:59 AM, Budyanto H.
[email protected] wrote:

What is really the difference between “singlethread” and “threadsafe”
Context Instance Type?

Threadsafe uses ThreadLocal to avoid race condition, but singlethread
does nothing. Singlethread is not thread safe. This model is based on
the assumption that only one thread works behind a script engine. In
some cases, singlethread is enough, while threadsafe is necessary on
other cases such as J2EE.

Does this affect the thread safety of the ScriptEngine’s eval() method
or invokeFunction() or both?

It depends on your case. Please see the thread that engine instance is
created on and the thread that those methods are executed on. If
exactly the same thread will be used always in both, singlethread is
enough.
Examples of the wiki page,
http://kenai.com/projects/jruby/pages/RedBridgeServletExamples, might
help you to understand the difference of local context models.

Unrelated question. Does invokeFunction actually parses the script
again? Or does parsing only occur once during the eval() call?

No. invokeFunction() method just calls Ruby methods. If you eval()
once, you don’t need to eval() anymore. However, eval() and
invokeFunction() must be done on the same thread. This is because Ruby
runtime memorizes the evaluated Ruby methods.

-Yoko

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks for the explanation. It makes more sense now.

Budyanto