【并发编程】AIDL关键字
Oneway interfaces
In early betas, the Android IPC was strictly synchronous. This means that service invocations had to wait for the return value of the remote method to arrive back to the caller. This is generally an advantage because the caller can be sure that the called service received the invocation by the time the remote method returns. In some cases, however, this causes the caller to wait unnecessarily. If synchronicity is not required and the method has no return value, oneway AIDL interfaces may be used.
Oneway methods are specified by addind the oneway keyword to the AIDL interface definition.
package com.elfylin;
oneway interface IMyServiceOneway {
String getValue();
}
package com.elfylin;
public interface IMyServiceOneway extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.elfylin.IMyServiceOneway
{
private static final java.lang.String DESCRIPTOR = "com.elfylin.IMyServiceOneway";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.elfylin.IMyServiceOneway interface,
* generating a proxy if needed.
*/
public static com.elfylin.IMyServiceOneway asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.elfylin.IMyServiceOneway))) {
return ((com.elfylin.IMyServiceOneway)iin);
}
return new com.elfylin.IMyServiceOneway.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getValue:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getValue();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.elfylin.IMyServiceOneway
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public java.lang.String getValue() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getValue, _data, null, android.os.IBinder.FLAG_ONEWAY);
}
finally {
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getValue = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String getValue() throws android.os.RemoteException;
}
in、out与inout
First, for all non-primitive parameters, you need to specify one of three directional types: in, out, or inout.
The in type indicates that they are used only for input and that your client won’t see any changes that the
Service does to this object. The out type indicates that the input object contains no relevant data but will
be populated with data by the Service that’s relevant in the response from the method. The inout type
is a combination of both types. It’s very important to use only the type that’s needed because there’s a cost
associated with each type.
Another thing to remember is that for all custom classes used in communication, you need to create an AIDL file
that declares your class as a Parcelable.
在使用aidl传输数据时,对于非基本数据类型,也不是String和CharSequence类型的(即Parcelable类型),需要有方向指示,包括in、out和inout。
下表为in和out在远程传输中的作用。
| Stub.ontransact() | Proxy.callback(Data data) | ||
|
接收远程传输的数据(Data) | 输入本地数据(Data) | |
| 中间过程 | 本地调用(修改Data) | 远程调用(给远程传输Data) | |
|
将经过本地调用修改过后的Data,返回给远端 |
获取远程调用之后,传输过来的远端数据(Data) |
a、server in client in
03-07 14:23:13.250: I/System.out(16307): client in: ZLData [data=Client]
03-07 14:23:13.250: I/System.out(16579): Server in: ZLData [data=Client]
03-07 14:23:13.250: I/System.out(16579): Server out:ZLData [data=server]
03-07 14:23:13.250: I/System.out(16307): client out: ZLData [data=Client] b、server in client out
03-07 14:22:00.980: I/System.out(16009): client in: ZLData [data=Client]
03-07 14:22:00.980: I/System.out(16050): Server in: null
03-07 14:22:00.980: I/System.out(16050): Server out:ZLData [data=server]
03-07 14:22:00.980: I/System.out(16009): client out: ZLData [data=Client] c、server out client in
03-07 14:22:37.170: I/System.out(16307): client in: ZLData [data=Client]
03-07 14:22:37.170: I/System.out(16421): Server in: ZLData [data=]
03-07 14:22:37.170: I/System.out(16421): Server out:ZLData [data=server]
03-07 14:22:37.170: I/System.out(16307): client out: ZLData [data=Client] d、server out client out
03-07 14:21:15.640: I/System.out(8592): client in: ZLData [data=Client]
03-07 14:21:15.640: I/System.out(15762): Server in: ZLData [data=]
03-07 14:21:15.640: I/System.out(15762): Server out:ZLData [data=server]
03-07 14:21:15.640: I/System.out(8592): client out: ZLData [data=server]
总结
- 如果client不需要传输数据给server,client只需要处理经过server处理过后的数据,那么client和server都为out。
- 如果client只需要传输数据给server,而不需要处理返回的数据,那么client和server都为in。
- 如果client需要传输数据给server,而且需要处理返回的数据,则client和server都为inout。
in,out会影响进程间传输的数据完整性。具体详细可查看配置不同的in、out 的情况下,aidl生成对应的java文件中Stub.ontransact() 和Proxy.yourMethod();
【并发编程】AIDL关键字的更多相关文章
- Java并发编程 Volatile关键字解析
volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了 ...
- 并发编程之关键字(synchronized、volatile)
并发编程主要设计两个关键字:一个是synchronized,另一个是volatile.下面主要讲解这两个关键字,并对这两个关机进行比较. synchronized synchronized是通过JMV ...
- 并发编程——synchronized关键字的使用
前言 我们一般对共享数据操作的时候,为了达到线程安全我们会使用synchronized关键字去修饰方法或者代码块.那么今天我们就来讲一讲synchronized关键字的使用. 专栏推荐: 并发编程专栏 ...
- Java并发编程volatile关键字
volatile理解 Java语言是支持多线程的,为了解决线程并发的问题,在语言内部引入了 同步块 和volatile 关键字机制.volatile具有synchronized关键字的“可见性”,vo ...
- Java并发编程1--synchronized关键字用法详解
1.synchronized的作用 首先synchronized可以修饰方法或代码块,可以保证同一时刻只有一个线程可以执行这个方法或代码块,从而达到同步的效果,同时可以保证共享变量的内存可见性 2.s ...
- Java并发编程_synchronized关键字的用法(一)
synchronized:意思是 同步,也就是 共享资源 Synchronized修饰方法:对象锁 Static Synchronized修饰方法:类锁 下面代码手动敲一遍,就会理解 一.Synch ...
- 并发编程-synchronized关键字大总结
0.synchronized 的特点: 可以保证代码的原子性和可见性. 1.synchronized 的性质: 可重入(可以避免死锁.单个线程可以重复拿到某个锁,锁的粒度是线程而不是调用).不可中断( ...
- java并发编程 volatile关键字 精准理解
1.volatile的作用 一个线程共享变量(类的成员变量.类的静态成员变量等)被volatile修饰之后,就具有以下作用: 1)并发中的变量可见性(不同线程对该变量进行操作时的可见性),即一个线程修 ...
- Java并发编程_volatile关键字的用法(二)
被volatile修饰的变量能够保证每个线程能够获取该变量的最新值,从而避免出现数据脏读的现象. 根据下面实例理解: package sync; public class VolatileTest e ...
- Java并发编程:volatile关键字解析
Java并发编程:volatile关键字解析 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...
随机推荐
- ●BZOJ 2251 [2010Beijing Wc]外星联络
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2251 题解: 后缀数组,倍增,RMQ 题意:把重复次数超过 1次的子串按字典序输出它们重复的 ...
- Simpson积分(BZOJ2178)
lrj的代码常数太大T了QAQ,改了一下. #include <cstdio> #include <cmath> #include <algorithm> usin ...
- spring 自定义事件发布及监听(简单实例)
前言: Spring的AppilcaitionContext能够发布事件和注册相对应的事件监听器,因此,它有一套完整的事件发布和监听机制. 流程分析: 在一个完整的事件体系中,除了事件和监听器以外,还 ...
- C语言程序设计第四次作业--选择结构(2)
(一)改错题 输出三角形的面积和周长,输入三角形的三条边a.b.c,如果能构成一个三角形,输出面积area和周长perimeter(保留2位小数):否则,输出"These sides do ...
- 《Java技术》第一次作业
(一)学习总结 1.在java中通过Scanner类完成控制台的输入,查阅JDK帮助文档,Scanner类实现基本数据输入的方法是什么?不能只用文字描述,一定要写代码,通过具体实例加以说明. (1)使 ...
- Linux允许、禁止ping包
默认情况下Linux系统允许ping,但是在某些情况下为了安全起见,我们都把服务器设置为禁ping 临时允许ping命令可使用命令: echo 0 >/proc/sys/net/ipv4/ic ...
- 光电转研发:和计算机没有一点关系的专业怎么去bat类的公司
光电 女 其实编码能力一般般,拿到百度腾讯研发offer. 一来幸运,二来真的想说行动决定了结果.研一没事就出去家教充实自己赚点钱,研二就开始找实习,去了网易,海康威视,百度实习.感觉还是吃了不少苦的 ...
- 锐捷Linux版的下载和使用(福大客户端)
下载锐捷程序包 点此下载 没有连接到锐捷里就进不了这个安装包的官方下载界面(好矛盾啊这个),所以我把它上传到博客园了. 解压文件 schaepher:~$ cd Downloads/ schaephe ...
- Java 反射详解 转载
java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实例化Class类对象 ...
- Tomcat性能调优-JVM监控与调优
参数设置 在Java虚拟机的参数中,有3种表示方法用"ps -ef |grep "java"命令,可以得到当前Java进程的所有启动参数和配置参数: 标准参数(-),所有 ...