




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、JNI 中基本類型數(shù)組的傳遞方法(無需拷貝數(shù)據(jù)!)0、先來看一下主要用到哪些函數(shù):C 代碼 GetIntArrayElements();/ 貌似得到的是副本,要拷 貝數(shù)據(jù) ReleaseIntArrayElements();/ 對應上面的函數(shù)的釋 放資源的函數(shù) env->GetPrimitiveArrayCritical();/ 貌似 得到的是指向原數(shù)據(jù)的指針 env->ReleasePrimitiveArrayCritical();/ 對應上面的函數(shù) 的釋放資源的函數(shù)官方文檔: spec/functions.html#wp17440JNI 函數(shù)的中譯本 (貌似沒看到 GetPri
2、mitiveArrayCritical (): 下面正式開始: 1、不知道如 何設置 JNI 環(huán)境的先看這里: 、 Java 端程序:Java 端:import java.util.Arrays;Java 代碼 package tests; public class TestJNIArraystaticSystem.loadLibrary(TestJNIArray); public static native voidaddOne(int ints);/ 數(shù)組元素 1 public static native void addOne(double ints);/ 數(shù)組元素 1 ,為了測試, C
3、 中 循環(huán)了 5 次 public static native int getNewArray(int size,int initValue);/ 生成初始值為 initValue 的數(shù)組,數(shù)組長 度為 sizepublic static void main(String args)throws InterruptedException int n=20; final int a=new intn;for (int i = 0; i< a.length; i ) ai=i; if(n<50)System.out.println(Arrays.toString(a); addOne(
4、a);if(n<50)System.out.println(Arrays.toString(a);final double d=new doublen;for (int i = 0; i <d.length; i ) di=i; /addOne(d);if(n<50)System.out.println(Arrays.toString(d); new Thread(new Runnable() Overridepublic void run() addOne(d); ).start();for (int i = 0; i <200; i ) Thread.sleep(2
5、0);System.out.println(Arrays.toString(d); if(dd.length-1-d0!=n-1)System.out.println( 檢測到 C 端 更新數(shù)據(jù)中 );/ 看看能否找到在 C 更新數(shù)組時 Java 端又讀 取數(shù)據(jù)的情況 int b=getNewArray(2, 9);System.out.println(Arrays.toString(b);int c=getNewArray(0, 9);System.out.println(Arrays.toString(c); 3、 C 端程序:C 代碼 #include tests_TestJNIArra
6、y.h#include<windows.h>#include <time.h>#include<iostream> #include <string>classTimer private:clock_t time; public:Timer() time=clock(); clock_t getElapsedTime()return clock()-time;clock_t getElapsedTimeAndRestart() tmp=time; time=clock();returnclock_t int return JNIEXPORT vo
7、idtime-tmp; void restart() time=clock(); getCLOCKS_PER_SEC() CLOCKS_PER_SEC; ;JNICALL Java_tests_TestJNIArray_addOne_3I( JNIEnv* env, jclass, jintArray intArray) jboolean b;Timer timer; jint* pint=env->GetIntArrayElements(intArray,&b);/ 獲取 指針!第二個參數(shù)可為 NULL std:string str=b?true:false; long t=t
8、imer.getElapsedTimeAndRestart(); std:cout<<GetIntArrayElements() 耗時 : <<t<<t 為副本? <<str<<std:endl;/ 很悲 劇,自己得到的是副本 (copy 了一份, 速度慢啊) jsizesize=env->GetArrayLength(intArray);for (int i=0;i<size; i ) pinti =1;env->ReleaseIntArrayElements(intArray,pint,0);/ 釋放 / 對于最
9、后一個參數(shù) (如果指針指向的數(shù)組為副 本時,否則該參數(shù)不起作用 ) / 0 copy back the content and free the elems buffer /JNI_COMMITcopy back the content but do not freethe elems buffer/JNI_ABORTfreethe buffer without copying back the possible changes std:cout<< 從 c 程序返回 <<std:endl; JNIEXPORT void JNICALL Java_tests_TestJ
10、NIArray_addOne_3D( JNIEnv * env, jclass, jdoubleArray intArray) jboolean b; Timer timer; double* pd=(double*)env->GetPrimitiveArrayCritical(intArray,&a mp;b); /if(pd=NULL)return; 理論上應該檢查! ! long t=timer.getElapsedTimeAndRestart(); jsize size=env->GetArrayLength(intArray);std:stringstr=b?true:f
11、alse; std:cout<<GetPrimitiveArrayCritical() 耗時 : <<t<<t 為副本? <<str<<std:endl;/ 這次 是原始數(shù)據(jù)了, happy 啊!for(int j=0;j<5;j ) /驗證一下, Java 中的數(shù)據(jù)也在更新! ! Sleep(1000);for (int i=0; i<size; i )pdi=1; Sleep(10); env->ReleasePrimitiveArrayCritical(intArray,pd,0);/ 別忘 了釋放 雖然不知道
12、不釋放有什么問題。 。 std:cout<< 從 c 程序返回 <<std:endl; JNIEXPORT jintArray JNICALL Java_tests_TestJNIArray_getNewArray( JNIEnv * env,jclass, jint size, jint initValue ) jintArray intArray=env->NewIntArray(size); jboolean* pb;/jint* pint=env->GetIntArrayElements(intArray,pb);jint* pint=new jintsize;std:stringstr=*pb?true:false; std:cout<< 自己生成的 Java 數(shù)組,取出指針,為副本? <<str<<std:
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- NB/T 11637-2024煤礦瓦斯抽采系統(tǒng)管理規(guī)范
- NB/T 11627-2024智慧礦山評價指標體系
- 2025年信息展示與傳播技術考試題及答案
- 智能財稅試題及答案
- 服裝面試測試題庫及答案
- 山西省大同市渾源縣第七中學校2024-2025學年高一下學期第一次月考 數(shù)學試題(含解析)
- 一級計算機考試題及答案
- 軟考網(wǎng)絡工程師新方式技能考核試題及答案
- 軟件設計師考試案例分析方法與試題與答案
- 網(wǎng)絡流量監(jiān)測中的挑戰(zhàn)與最佳實踐試題及答案
- 游客自愿離團協(xié)議書
- 熱射病護理試題及答案
- 2024-2025學年滬教版(五四學制)七年級英語下學期考點突破:書面表達15篇(含答案)
- 小區(qū)中控室面試題及答案
- 交規(guī)考試題庫
- 選擇性必修1 《當代國際政治與經(jīng)濟》(主觀題答題模版)
- 量子光子ics在生物醫(yī)學中的應用-全面剖析
- 2025年度手術室感染控制工作計劃
- 課標視角下數(shù)學跨學科項目式學習案例設計與思考
- 國開《離散數(shù)學》大作業(yè)及答案
- 離婚協(xié)議書 標準版電子版(2025年版)
評論
0/150
提交評論