On Feb 6, 10:55 am, [EMAIL PROTECTED] wrote: > Hello, > > I am trying to embed JavaScript into my Java application, and got a > bit confused with the contexts. > > JavaDoc for Context.enter() says this: > > "Instead of using enter(), exit() pair consider using > call(ContextAction) which guarantees proper association of Context > instances with the current thread and is faster." > > And the doc for call(ContextAction) says this: > > "If no Context is associated with the thread, then > ContextFactory.getGlobal().makeContext() will be called to construct > new Context instance. The instance will be temporary associated with > the thread during call to ContextAction.run(Context)." > > What confuses me is the word "temporary". Does that mean that it is > not guaranteed that an already existing context (previously allocated > for the current thread) will be re-used?
No, if there is already a Context associated with the thread it will be reused. > What is the proper way to > avoid allocation of more than one context per thread? ContextFactory.call will take care of this, only creating a new Context when one is not already associated with the thread. > > Also, when do I need to call Context.exit()? Is it reasonable to do a > Context.enter() once at the start of the thread that will execute > JavaScript and never exit it? If you use ContextFactory.call, you don't need to worry about calling exit() -- it will be called for you. If you use another method for associating a Context with a thread (like ContextFactory.enterContext) you should call Context.exit(). At the very least, there may be some onContextReleased actions that would be good to run. Calling exit() also clears a thread local reference to the context. > > Thanks in advance, > Ivan. _______________________________________________ dev-tech-js-engine-rhino mailing list [email protected] https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
