【并发编程】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之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...
随机推荐
- ●POJ poj 2112 Optimal Milking
●题目大意: 给出K个挤奶机器(编号1~K),C头牛(编号K+1~K+C)(机器和牛各在不同的地方)和每台机器最多可M头牛挤奶: 然后以邻接矩阵告诉各点间的直接距离(不同的地方间若直接距离等于0,则表 ...
- 【HDU 2669】Romantic
Problem Description The Sky is Sprite.The Birds is Fly in the Sky.The Wind is Wonderful.Blew Throw t ...
- 【bzoj4567 scoi2016】 背单词
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...
- 51Nod 1781 跑的比谁都快
香港记者跑的比谁都快是众所周知的常识. 现在,香港记者站在一颗有 n 个点的树的根结点上(即1号点),编号为 i 的点拥有权值 a[i] ,数据保证每个点的编号都小于它任意孩子结点的别号. 我们假定这 ...
- POJ 3294 n个串中至少一半的串共享的最长公共子串
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12484 Accepted: 3502 Descr ...
- Go实现海量日志收集系统(三)
再次整理了一下这个日志收集系统的框,如下图 这次要实现的代码的整体逻辑为: 完整代码地址为: https://github.com/pythonsite/logagent etcd介绍 高可用的分布式 ...
- 笔记11 在XML中声明切面(2)
为通知传递参数 1.声明一个CompactDiscs接口.内部包含两个方法: show() 用于显示唱片的名字和艺术风格 playTrack(int number) 根据传入的磁道数播放相应磁道的音乐 ...
- Ubuntu安装与配置KVM
事前检查 查看一下linux是32位还是64位 file /bin/ls 确认一下 CPU支持硬件虚拟化(不支持也没关系,可以继续) egrep -o '(vmx|svm)' /proc/cpuinf ...
- go优化
1.安装graphviz软件,安装步骤如下::(1)安装graphvizyum install 'graphviz*' (2)查看已安装graphviz包yum list 'graphviz*' 2. ...
- 容器化现有ASP.NET MVC 5应用
.NET Core的出现使得ASP.NET应用在Linux环境下使用变得更加普及.而配合上Docker容器,令ASP.NET应用的布署与管理也变得更加方便.在新的项目中运用ASP.NET Core无可 ...