犀牛嵌入
有人了解 rhino javascript Contexts 吗?我找不到任何有关它的有用文档。我的主要问题是 Context.exit() (实际上应该是 cx.exit()),据我了解,它退出与当前线程关联的上下文?这是否意味着我需要跟踪哪个线程做了什么?
主线程:
Context cx;
cx.evaluateReader( ... ) // load some function
start thread 2
线程2:
Object o= scope.get("methodname", scope);
((Function)o).call( ... )
我不打算进行多线程处理,但是如果不同的设置来自不同的线程怎么办?
Anyone understands the rhino javascript Contexts? I cannot find any useful documentation about it. My main problem is the Context.exit() (really should be cx.exit()) which from what I understand exits the context associated with the current thread? Does that mean I need to keep track of what which thread does?
main thread:
Context cx;
cx.evaluateReader( ... ) // load some function
start thread 2
thread 2:
Object o= scope.get("methodname", scope);
((Function)o).call( ... )
I do not plan on doing multithreading but what if the different setups comes from different threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自网站文档:
换句话说,不要在线程之间传递上下文。只需在正在运行的线程中创建一个新的上下文即可。不必担心在线程内多次调用
Context.enter()
。它们实际上是内部引用计数的线程局部变量。因此,在同一个线程中调用 Context.enter() 非常简单。再次来自文档:
就我个人而言,我只是在任何地方都使用了这个代码构造:
事实上,在 Groovy 中我将这个组合在一起:
然后传递如下代码:
最后一点。范围不依赖于上下文,并且可以在线程之间保留/传递。
From the website docs:
In other words, do not pass the context between threads. Just create a new context in the running thread. Don't worry about calling
Context.enter()
more than once within a thread. They are effectively thread-local variables that are internally reference-counted. So callingContext.enter()
in the same thread is very light.Again from the docs:
Personally, I just used this code construct everywhere:
In fact, in Groovy I whipped together this:
and then pass it code like the following:
One final note. The scope is not tied to the context, and can be persisted/passed between threads.