【并发编程】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之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在 ...
随机推荐
- 洛谷P3164 [CQOI2014]和谐矩阵
高斯消元,可以直接消的 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cst ...
- [LSGDOJ1822]书架 Splay
题目描述 Sally有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. Sally在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一 ...
- ●BZOJ 2669 [cqoi2012]局部极小值
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2669 题解: 容斥,DP,DFS 先看看 dp 部分:首先呢,X的个数不会超过 8个.个数很 ...
- 初遇 Asp.net MVC 数据库依赖缓存那些事儿
问题背景: 最近做一个非常简单的功能,就是使用ajax请求的方式从服务端请求一段下拉表的数据. 以前也有做过这个功能,只不过这次做这个功能的时候冒出了一个想法: 我请求的这段数据它是一段相对比较固定的 ...
- Postgres中postmaster代码解析(中)
今天我们对postmaster的以下细节进行讨论: backend的启动和client的连接请求的认证 客户端取消查询时的处理 接受pg_ctl的shutdown请求进行shutdown处理 2.与前 ...
- VMware下安装Linux(CentOs6.3)操作系统
VMware 10.0.2 CentOs 6.3 VMware的安装以及CentOs的下载比较简单,这里不再描述 1.创建新的虚拟机 2.选择典型 3.选择稍后安装操作系统 4.选择如图所示 5.虚拟 ...
- LAN、WAN、WLAN、WiFi之间的区别
感觉这几个概念让人傻傻分不清,下面以最常见的路由器来解释这几个概念. LAN 1 LAN,全称Local Area Network,中文名叫做局域网. 顾名思义,LAN是指在某一区域内由多台计算机 ...
- redis中密码设置
先打开redis-server 再打开redis-cli 在redis-cli对redis进行操作 可以通过编辑redis.conf配置文件来设置密码. 1.重启Redis设置密码: 在配置文件中有个 ...
- BlockingQueue阻塞队列(解决多线程中数据安全问题 可用于抢票,秒杀)
案例:一个线程类中 private static BlockingQueue<Map<String, String>> dataQueue = new LinkedBlocki ...
- C# 获取当前屏幕DPI
1.通过Graphics类获取 Graphics currentGraphics = Graphics.FromHwnd(new WindowInteropHelper(mainWindow).Han ...