1. 程式人生 > >【併發程式設計】AIDL關鍵字

【併發程式設計】AIDL關鍵字

oneway
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)
in 

接收遠端傳輸的資料(Data)輸入本地資料(Data)
中間過程本地呼叫(修改Data)遠端呼叫(給遠端傳輸Data)
out
將經過本地呼叫修改過後的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關鍵字

onewayOneway interfaces In early betas, the Android IPC was strictly synchronous. This means that service invocations had to wait for the return value of t

併發程式設計CPU cache結構和快取一致性(MESI協議)

一、cache cpu cache已經發展到了三級快取結構,基本上現在買的個人電腦都是L3結構。 1. cache的意義 為什麼需要CPU cache?因為CPU的頻率太快了,快到主存跟不上,這樣在處理器時鐘週期內,CPU常常需要等待主存,浪費資源。所以cac

併發程式設計對阻塞/非阻塞、同步/非同步、併發/並行等基本概念的理解

1. 併發與並行 併發:concurrency 並行:parallelism 開發過程中,常常會接觸併發有關的概念,比如併發計算(concurrent computing),併發系統( concurrent system),併發控制(concurrent

併發程式設計 作業系統介紹

目錄 一、為什麼要有作業系統 二、什麼是作業系統 三、作業系統與普通軟體的區別 四、作業系統發展史    一、為什麼要有作業系統 現代的計算機系統主要是由一個或者多個處理器,主存,硬碟,鍵盤,滑鼠,顯示器,印表機,網路介面及其他輸入輸出裝置組成。   

併發程式設計多執行緒

目錄 一、什麼是執行緒 二、開啟執行緒的兩種方式 三、多執行緒與多程序的區別 四、守護執行緒 一、什麼是執行緒 1.1 概念 執行緒是程序的一個實體,是CPU排程和分派的基本單位,它是比程序更小的能獨立執行的基本單位。執行緒自己基本上不擁有系統資源,只擁有一點在執行中必不可少的資源(如程式計

併發程式設計IO模型

  一、要點回顧 為了更好地瞭解IO模型,我們需要先回顧下幾個概念:同步、非同步、阻塞、非阻塞 同步: 一個程序在執行某個任務時,另外一個程序必須等待其執行完畢,才能繼續執行。就是在發出一個功能呼叫時,在沒有得到結果之前,該呼叫就不會返回。按照這個定義,其實絕大多數函式都是同步呼叫。但是一般

併發程式設計通過命令列獲取執行緒資訊

命令adb shell ps-t 檢視程序中執行緒的資訊-x 檢視utime和stime-P 檢視屬性-p 檢視排程策略,通常是檢視一個app處於前臺還是後臺-c 檢視哪一個CPU在執行這個程序name|pid 用名字或pid過濾例子(1) 檢視包名為com.eat的執行緒,

併發程式設計Future模式新增Callback及Promise 模式

Future Future是Java5增加的類,它用來描述一個非同步計算的結果。你可以使用 isDone 方法檢查計算是否完成,或者使用 get 方法阻塞住呼叫執行緒,直到計算完成返回結果。你也可以使用 cancel 方法停止任務的執行。下面來一個

併發程式設計 圖文深入解析Java顯示鎖底層原始碼 —— 加解鎖是如何實現的

一、瞭解 AbstractQueuedSynchronizer(AQS) 1、AQS 簡介 AbstractQueuedSynch

併發程式設計 圖文深入解析Java顯式鎖底層原始碼 —— condition 實現執行緒排程

一、回顧 AQS 資源的鎖定與釋放 上篇文章(文章中有詳細的原始碼解讀) 說到,AQS 在 tryRelease 失敗後,資源的鎖

併發程式設計一文帶你讀懂深入理解Java記憶體模型(面試必備)

併發程式設計這一塊內容,是高階資深工程師必備知識點,25K起如果不懂併發程式設計,那基本到頂。但是併發程式設計內容龐雜,如何系統學

併發程式設計Volatile原理和使用場景解析

目錄 一個簡單列子 Java記憶體模型 快取不一致問題 併發程式設計中的“三性” 使用volatile來解決共享變數可見性 volatile和指令重排(有序性

併發程式設計ThreadLocal其實很簡單

什麼是ThreadLocal ThreadLocal有點類似於Map型別的資料變數。ThreadLocal型別的變數每個執行緒都有自己的一個副本,某個執行緒對這個變數的修改不會影響其他執行緒副本的值。需要注意的是一個ThreadLocal變數,其中只能set一個值。 ThreadLocal<String

併發程式設計synchronized的使用場景和原理簡介

1. synchronized使用 1.1 synchronized介紹 在多執行緒併發程式設計中synchronized一直是元老級角色,很多人都會稱呼它為重量級鎖。但是,隨著Java SE 1.6對synchronized進行了各種優化之後,有些情況下它就並不那麼重了。 synchronized可以修飾普

併發程式設計Java中的原子操作

什麼是原子操作 原子操作是指一個或者多個不可再分割的操作。這些操作的執行順序不能被打亂,這些步驟也不可以被切割而只執行其中的一部分(不可中斷性)。舉個列子: //就是一個原子操作 int i = 1; //非原子操作,i++是一個多步操作,而且是可以被中斷的。 //i++可以被分割成3步,第一步讀取i的值,

併發程式設計摩爾定律失效“帶來”並行程式設計

併發和並行 在真正開始聊本文的主題之前,我們先來回顧下兩個老生常談的概念:併發和並行。 併發:是指多個執行緒任務在同一個CPU上快速地輪換執行,由於切換的速度非常快,給人的感覺就是這些執行緒任務是在同時進行的,但其實併發只是一種邏輯上的同時進行; 並行:是指多個執行緒任務在不同CPU上同時進行,是真正意義上

併發程式設計併發程式設計中你需要知道的基礎概念

本部落格系列是學習併發程式設計過程中的記錄總結。由於文章比較多,寫的時間也比較散,所以我整理了個目錄貼(傳送門),方便查閱。 併發程式設計系列部落格傳送門 多執行緒是Java程式設計中一塊非常重要的內容,其中涉及到很多概念。這些概念我們平時經常掛在嘴上,但是真的要讓你介紹下這些概念,你可能還真的講不清楚。這

併發程式設計Thread類的詳細介紹

本部落格系列是學習併發程式設計過程中的記錄總結。由於文章比較多,寫的時間也比較散,所以我整理了個目錄貼(傳送門),方便查閱。 併發程式設計系列部落格傳送門 Thread類是Java中實現多執行緒程式設計的基礎類。本篇部落格就來介紹下Thread類的常用API和常見用法。 Thread類常用的方法如下:

併發程式設計Object的wait、notify和notifyAll方法

本部落格系列是學習併發程式設計過程中的記錄總結。由於文章比較多,寫的時間也比較散,所以我整理了個目錄貼(傳送門),方便查閱。 併發程式設計系列部落格傳送門 方法簡介 wait方法 wait方法是Object類中的一個方法。呼叫這個方法會讓呼叫執行緒進入waiting狀態,直到另一個執行緒呼叫了當前物件上的

併發程式設計ThreadLocal的兄弟InheritableThreadLocal

本部落格系列是學習併發程式設計過程中的記錄總結。由於文章比較多,寫的時間也比較散,所以我整理了個目錄貼(傳送門),方便查閱。 併發程式設計系列部落格傳送門 引子 public class InheritableThreadLocalDemo { private static ThreadLoca