犀牛嵌入

发布于 2024-08-17 15:36:00 字数 398 浏览 2 评论 0原文

有人了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一腔孤↑勇 2024-08-24 15:36:00

来自网站文档

Rhino Context 对象用于存储有关执行环境的线程特定信息。应该有且仅有一个与将执行 JavaScript 的每个线程关联的上下文。

换句话说,不要在线程之间传递上下文。只需在正在运行的线程中创建一个新的上下文即可。不必担心在线程内多次调用 Context.enter() 。它们实际上是内部引用计数的线程局部变量。因此,在同一个线程中调用 Context.enter() 非常简单。

再次来自文档

即使已经存在与当前线程关联的上下文,这些调用也将正常工作。该上下文将被返回,并且内部计数器会递增。只有当计数器为零时才会与线程解除关联。

就我个人而言,我只是在任何地方都使用了这个代码构造:

Context ctx = Context.enter();
try {
    // do something with the ctx
} finally {
    Context.exit();
}

事实上,在 Groovy 中我将这个组合在一起:

def withContext(Closure closure) {
    Context ctx = Context.enter();
    try {
        closure.call(ctx);
    } finally {
        Context.exit();
    }
}

然后传递如下代码:

withContext { Context ctx ->
    ScriptableObject scope = ctx.initStandardObjects()
    // now to do work with the scope and ctx.
}

最后一点。范围不依赖于上下文,并且可以在线程之间保留/传递。

From the website docs:

The Rhino Context object is used to store thread-specific information about the execution environment. There should be one and only one Context associated with each thread that will be executing JavaScript.

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 calling Context.enter() in the same thread is very light.

Again from the docs:

These calls will work properly even if there is already a Context associated with the current thread. That context will be returned and an internal counter incremented. Only when the counter reaches zero will it be disassociated from the thread.

Personally, I just used this code construct everywhere:

Context ctx = Context.enter();
try {
    // do something with the ctx
} finally {
    Context.exit();
}

In fact, in Groovy I whipped together this:

def withContext(Closure closure) {
    Context ctx = Context.enter();
    try {
        closure.call(ctx);
    } finally {
        Context.exit();
    }
}

and then pass it code like the following:

withContext { Context ctx ->
    ScriptableObject scope = ctx.initStandardObjects()
    // now to do work with the scope and ctx.
}

One final note. The scope is not tied to the context, and can be persisted/passed between threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文