在等待响应时,GWT AsyncCallback 会执行多远?

发布于 2024-10-18 02:43:01 字数 666 浏览 4 评论 0原文

如果我从一个函数(全部用 Java 编写)调用:

public int hello() {
int a = 1;
executeCallback();
// C: Question lies in this range
return a;
}

public void executeCallback() {
// A: random code to execute before asynccallback
   randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess();
      onFailure();
   });
// B: random code to execute after asynccallback
}

我知道注释 A 中的内容将执行,同时非同步 randomMethod 将执行,B 中的注释将执行。

不过,我想知道,当 randomMethod 执行时(如果需要足够长的时间),函数是否会返回到其调用者(在本例中为方法“hello”)并开始执行注释 C 中的代码?或者executeCallback会等待randomMethod完成后再返回?

如果是前者,假设我需要 randomMethod 触及的信息才能继续评论 C,我怎样才能让它“等待”以确保情况如此?

If I were to call from a function (all written in Java):

public int hello() {
int a = 1;
executeCallback();
// C: Question lies in this range
return a;
}

public void executeCallback() {
// A: random code to execute before asynccallback
   randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess();
      onFailure();
   });
// B: random code to execute after asynccallback
}

I understand that the stuff in comment A will execute, and concurrently the non synchronous randomMethod will execute and the comment in B will execute.

I was wondering, though, while randomMethod is executing (if it takes sufficiently long), will the function return to its caller (in this case the method 'hello') and start executing the code in comment C? Or will executeCallback wait for randomMethod to finish before it returns?

And if it's the former, assume I need the information that randomMethod touches to be touched before I can continue on to comment C, how can I make it 'wait' to ensure that this will be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

宁愿没拥抱 2024-10-25 02:43:01

当调用异步方法时,程序不会等待该方法,这就是它们被称为异步的原因。 randomMethod AsyncCallback的onSuccess或OnFailure方法不可能在B表示的代码之前执行。因为,浏览器在单线程中执行javascript代码,onSuccess或OnFailure方法在executeCallBack方法的调用者完成之后执行。

如果你想让代码B和代码C在randomMethod之后执行,你应该将它们放在Success方法上,例如;

randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess() {
         // B: random code to execute after asynccallback
        // C: Question lies in this range
        }
      }
      onFailure()
   });

When asynchronous method is called, program does not wait that method , that is why they are called asynchronous. There is no way that randomMethod AsyncCallback onSuccess or OnFailure methods are executed before the code represented as B.. Because , the browser executes javascript code in a single thread, onSuccess or OnFailure methods is executed after the caller of executeCallBack method finish.

If you want code B and code C are executed after randomMethod , you should put them onSuccess method, such as ;

randomClass.randomMethod(int a, int b, AsyncCallback<ReturnType>() {
      onSuccess() {
         // B: random code to execute after asynccallback
        // C: Question lies in this range
        }
      }
      onFailure()
   });
GRAY°灰色天空 2024-10-25 02:43:01

让我稍微解释一下执行模型,因为如果您知道“幕后”发生了什么,有时会更容易。

所有代码的执行都是由浏览器发起的。这会在某些事件发生时发生,例如,当页面加载时、当用户单击某些内容时或当 AJAX 响应到达时。

因此,在编写 GWT(或其他 JavaScript)应用程序时,您要做的就是注册处理程序,例如使用 onModuleLoad(),或者将 ClickHandler 注册到 Button,或者将 AsyncCallback 注册到 GWT -RPC 调用。

有趣的(也许是违反直觉的?)事情是,当浏览器调用这样的处理程序时,它会被执行直到完成(或者直到发生错误)。只有在那之后,其他处理程序才会被执行。 顺便说一句,这也意味着,如果一个处理程序的代码包含无限循环,则其他回调将永远不会被执行 - 整个浏览器选项卡将被阻塞。

因此,当您的 hello()< /code> 方法被执行,这是作为某些处理程序(例如 onModuleLoad 或 ClickHandler)的一部分完成的。它

  • 设置a = 1
  • 然后发送一个AJAX请求(randomClass.randomMethod),
  • 注册另一个AsyncCallback处理程序(记住,这只是一个注册浏览器将首先完成当前处理程序,然后才能实际执行该 AsyncCallback 处理程序)。
  • 执行 B
  • 执行 A
  • 继续执行调用层次结构,直到处理程序完成。

Let me explain the execution model a bit, as it's sometimes easier if you know what happens "behind the scenes".

All code execution is initiated by the browser. This happens at certain events, e.g. when the page loads, when the user has clicked something, or when an AJAX response arrives.

So what you do when writing a GWT (or other JavaScript) application is, that you register handlers, e.g. by using onModuleLoad(), or by registering a ClickHandler to a Button, or an AsyncCallback to a GWT-RPC call.

The interesting (and maybe counter-intuitive?) thing is, that when the browser calls such a handler, it is executed till it finishes (or until an error occurs). Only after that, other handlers will be executed. This also means by the way, that if the code for one handler contains an endless loop, other callbacks will never be executed - the entire browser tab will block.

So when your hello() method is executed, this is done as part of some handler (e.g. onModuleLoad or a ClickHandler). It

  • sets a = 1,
  • then sends an AJAX request (randomClass.randomMethod),
  • registers another AsyncCallback handler (remember, this is just a registration. The browser will first finish the current handler, before it can get to actually executing that AsyncCallback handler).
  • executes B
  • executes A
  • continues executing up the call hierarchy, until the handler is finished.
兲鉂ぱ嘚淚 2024-10-25 02:43:01

1]
异步通信可能需要一点时间来适应。您需要记住,无论服务器上的过程执行以及从服务器到客户端的数据传输需要多长时间,客户端都会在调用远程过程后立即继续执行下一条语句。

2]你可以在onSuccess方法中执行 // C: 问题就在这个范围内

1]
asynchronous communication may take a little bit of time to get used to. You need to remember that the client continues to next statement right after the call to the remote procedure no matter how long the execution of the procedure on the server and data transmission from the server to the client takes.

2] You can execute // C: Question lies in this range in onSuccess method

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