Android .aidl中的单向声明是否保证在单独的线程中调用该方法? [英] Does oneway declaration in Android .aidl guarantee that method will be called in a separate thread?

查看:141
本文介绍了Android .aidl中的单向声明是否保证在单独的线程中调用该方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Android手机的客户端/服务器应用程序设计框架。我对Java和Android都很陌生(但对于一般的编程或者特别是线程编程来说并不陌生)。

I am designing a framework for a client/server application for Android phones. I am fairly new to both Java and Android (but not new to programming in general, or threaded programming in particular).

有时我的服务器和客户端会在同一个过程,有时它们将处于不同的过程中,具体取决于具体的用例。客户端和服务器接口类似于以下内容:

Sometimes my server and client will be in the same process, and sometimes they will be in different processes, depending on the exact use case. The client and server interfaces look something like the following:

IServer.aidl:

IServer.aidl:

package com.my.application;

interface IServer {

    /**
     * Register client callback object
     */
    void registerCallback( in IClient callbackObject );

    /**
     * Do something and report back
     */
    void doSomething( in String what );
  .
  .
  .
}

IClient.aidl:

IClient.aidl:

package com.my.application;

oneway interface IClient {

    /**
     * Receive an answer
     */
    void reportBack( in String answer );
  .
  .
  .
}

现在这里有趣的地方。我可以预见客户端调用 IServer.doSomething()的用例,然后调用 IClient.reportBack() ,并根据报告的内容, IClient.reportBack()需要再次发出 IClient.doSomething()

Now here is where it gets interesting. I can foresee use cases where the client calls IServer.doSomething(), which in turn calls IClient.reportBack(), and on the basis of what is reported back, IClient.reportBack() needs to issue another call to IClient.doSomething().

这里的问题是 IServer.doSomething()通常不会是可重入的。没关系,只要 IClient.reportBack()总是在新线程中调用。在这种情况下,我可以确保 IServer.doSomething()的实现始终是 synchronized ,以便从新线程块调用,直到第一个调用返回。

The issue here is that IServer.doSomething() will not, in general, be reentrant. That's OK, as long as IClient.reportBack() is always invoked in a new thread. In that case, I can make sure that the implementation of IServer.doSomething() is always synchronized appropriately so that the call from the new thread blocks until the first call returns.

如果一切按照我认为的方式工作,那么通过将IClient接口声明为单程,我保证是这样的。至少,我想不出任何方式从 IServer.doSomething() IClient.reportBack()可以立即返回(单向应该确保),但是 IClient.reportBack 仍然可以重新启动 IServer.doSomething 在同一个线程中递归。必须启动IServer中的新线程,否则旧的IServer线程可以重新用于对IServer.doSomething()的内部调用,但只能在外部调用 IServer.doSomething()之后已经退回。

If everything works the way I think it does, then by declaring the IClient interface as oneway, I guarantee this to be the case. At least, I can't think of any way that the call from IServer.doSomething() to IClient.reportBack() can return immediately (what oneway is supposed to ensure), yet IClient.reportBack still be able to reinvoke IServer.doSomething recursively in the same thread. Either a new thread in IServer must be started, or else the old IServer thread can be re-used for the inner call to IServer.doSomething(), but only after the outer call to IServer.doSomething() has returned.

所以我的问题是,一切都按我认为的方式运作吗? Android文档几乎没有提到 oneway 接口。

So my question is, does everything work the way I think it does? The Android documentation hardly mentions oneway interfaces.

推荐答案

oneway关键字意味着如果该调用导致IPC(即调用者和被调用者处于不同的进程中),那么调用进程将不会等待被调用进程处理IPC。如果它不导致IPC(即它们都在同一个过程中),则呼叫将是同步的。这是一个令人遗憾的细节,它简化了绑定器IPC的实现。如果它们在同一个进程中,则调用只是一个常规的java方法调用。

The oneway keyword means that if that call results in an IPC (i.e. the caller and callee are in different processes) then the calling process will not wait for the called process to handle the IPC. If it does not result in an IPC (i.e. they're both in the same process), the call will be synchronous. It's an unfortunate detail that simplifies the implementation of binder IPC a lot. If they're in the same process, the call is just a regular java method call.

这篇关于Android .aidl中的单向声明是否保证在单独的线程中调用该方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆