Android教程:使用线程构建模块编写多线程应用

hdsakjc 2014-03-14 03:19:12
近来,我们发布了“Windows* 8 教程: 使用英特尔 线程构建模块为 Windows 应用商店* 编写多线程应用”。 其中我们提到,并行计算引擎很容易导入其他移动或台式机平台。 Android 是这种移动平台的一个典型的例子。

在最新发布的英特尔 线程构建模块(英特尔 TBB)的稳定版本中,我们为 Android 应用增加了实验支持,即构建英特尔 TBB 库,通过 JNI 界面在 Android 应用中使用。您可以从threadingbuildingblocks.org中下载 该版本。

如要在 Linux* 主机上启动流程,请打开英特尔 TBB 源发布,找到<unpacked_dir>/build/android_setup.csh 脚本 并构建库。您必须构建库,因为开发版本仅可在源形式下发布。 <unpacked_dir>/build/index.android.html 文件中包括在 Linux 上配置环境和构建库的说明。

假定 %PATH%(在 Microsoft Windows* 主机平台上)和 $PATH (在 Linux 平台上)中 gnu make 3.81 可用,我们需要在 NDK 环境中执行下列命令来为 Android 构建英特尔 TBB 库:
gmake tbb tbbmalloc target=android
上述是构建库所需的操作;我们现在可以使用 Eclipse* 来构建示例。 对于下列的示例,我将使用 Windows* 的 Android SDK 工具 Rev.21 和 Android NDK Rev 8C 来说明跨平台开发的流程。

使用默认模板《New Android Application》创建新对象。 为简单起见,我们将其命名为“app1”,与上述的发布相同:


选择 FullscreenActivity 作为“活动”。 有关该模板的内容到此为止。 请注意不可以使用 com.example* 为 Google Play* 命名,但是它可以很好地帮助我们理解。

然后向主帧中添加几个按钮。 添加后,主帧的 XML 文件(app1/res/layout/activity_fullscreen.xml)如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".FullscreenActivity" >
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="74dp"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<Button
android:id="@+id/dummy_button1"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button1"
android:onClick="onClickSR" />
<Button
android:id="@+id/dummy_button2"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button2"
android:onClick="onClickDR" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
字符串文件(app1/res/values/strings.xml)如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sample</string>
<string name="dummy_content">Reduce sample</string>
<string name="dummy_button1">Simple Reduce</string>
<string name="dummy_button2">Deterministic Reduce</string>
</resources>
然后添加按钮处理器:

// JNI functions
private native float onClickDRCall();
private native float onClickSRCall();

public void onClickDR(View myView) {
TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));
float res=onClickDRCall();
tv.setText("Result DR is n" + res);
}


public void onClickSR(View myView) {
TextView tv=(TextView)(this.findViewById(R.id.fullscreen_content));
float res=onClickSRCall();
tv.setText("Result SR is n" + res);
}
然后,将库加载至 FullscreenActivity.java 文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

System.loadLibrary("gnustl_shared");
System.loadLibrary("tbb");
System.loadLibrary("jni-engine");
}
在使用 "tbb" 库时应清除所有内容,且需要使用 "gnustl_shared" 库来支持 TBB 的 C++ 语言特性。但是,对于 "jni-engine" 库,我们需要获得更多内容。

"jni-engine" 是 С++ 库,可执行计算引擎并为名为 onClickSRCall() 和 onClickSRCall() 的 JNI 调用输出 C 接口。

根据 NDK 开发规则,在工作区内创建文件夹 “jni” 并在该文件夹下专门针对 "jni-engine" 库创建 3 个文件。

这些文件是:

Android.mk ( <> 括号中的文本应使用实际的值替换)

LOCAL_PATH := $(call my-dir)
TBB_PATH := <path_to_the_package>

include $(CLEAR_VARS)
LOCAL_MODULE := jni-engine
LOCAL_SRC_FILES := jni-engine.cpp
LOCAL_CFLAGS += -DTBB_USE_GCC_BUILTINS -std=c++11 -I$(TBB_PATH)/include
LOCAL_LDLIBS := -ltbb -L./ -L$(TBB_PATH)/<path_to_libtbb_so>
include $(BUILD_SHARED_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := libtbb
LOCAL_SRC_FILES := libtbb.so
include $(PREBUILT_SHARED_LIBRARY)


Application.mk

APP_ABI := x86
APP_GNUSTL_FORCE_CPP_FEATURES := exceptions rtti
APP_STL := gnustl_shared


jni-engine.cpp:

#include <jni.h>

#include "tbb/parallel_reduce.h"
#include "tbb/blocked_range.h"
float SR_Click()
{
int N=10000000;
float fr = 1.0f/(float)N;
float sum = tbb::parallel_reduce(
tbb::blocked_range<int>(0,N), 0.0f,
[=](const tbb::blocked_range<int>& r, float sum)->float
{
for( int i=r.begin(); i!=r.end(); ++i )
sum += fr;
return sum;
},
[]( float x, float y )->float
{
return x+y;
}
);
return sum;
}

float DR_Click()
{
int N=10000000;
float fr = 1.0f/(float)N;
float sum = tbb::parallel_deterministic_reduce(
tbb::blocked_range<int>(0,N), 0.0f,
[=](const tbb::blocked_range<int>& r, float sum)->float
{
for( int i=r.begin(); i!=r.end(); ++i )
sum += fr;
return sum;
},
[]( float x, float y )->float
{
return x+y;
}
);
return sum;
}


extern "C" JNIEXPORT jfloat JNICALL Java_com_example_app1_FullscreenActivity_onClickDRCall(JNIEnv *env, jobject obj)
{
return DR_Click();
}


extern "C" JNIEXPORT jfloat JNICALL Java_com_example_app1_FullscreenActivity_onClickSRCall(JNIEnv *env, jobject obj)
{
return SR_Click();
}

请点击这个链接阅读详细内容:http://g.csdn.net/5266297
...全文
65 回复 打赏 收藏 转发到动态 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
相关推荐
       Android零基础入门这门课程的目标:带您走进Android大门,了解Android整体框架,演变发展历史 ;掌握Android编程的基础概念,教您构建各种应用程序:从Hello World开始,直到带有调度作业、更新设置、访问网络和使用架构组件的应用程序;课程结束后,能自己动手编写一定难度的Android程序,具备自学更高级开发的基础,能看懂开源的Android代码。      课程整体上分5个大的模块依次展开:      1.入门 (第1~3章)      2.用户体验(第4~6章)      3.在后台运行(第7~8章)      4.保存用户数据(第9~10章)      5.应用上架(第11章)。课程大纲如下:第1章. 构建您的第一个应用程序       1. 对Android有基本认识,搞懂Android系统整体框架,Android不同版本以及这些版本演变过程       2.以HelloWord为例,了解Android app的程序结构       3.Android UI的基础知识-视图和布局,初步介绍Android View的事件处理,及图片等资源的使用等第2章.  Activity和Intent       1.Activity 和 Intent 基础和用法       2.Activity生命周期和回调       3.Activity实例状态第3章. 测试应用 调试应用使用Support library       1.讲解Android两个主要的应用程序调试方法:日志 和 断点       2.如何对App进行单元测试       3.介绍Android中用于向后兼容的 Support library及用法第4章. 用户交互        讲解Android中常用控件的使用方法和注意事项,如按钮、输入控件、菜单、对话框、用户导航、列表等第5章. 令人愉快的用户体验      1.学会使用Drawables      2.使用Android studio中的图片和矢量图      3.讲解Android中主题和样式,以及如何定制主题和样式      4.Android著名的应用设计理念Material design,通过Material design打造极致的用户体验      5.如何让布局自适应不同尺寸的手机第6章. 界面测试        介绍Android UI自动化测试 Espresso第7章.后台任务        学会Android线程编程,使用非UI线程在后台完成耗时任务,从网络存取数据。撑握Android的广播和后台服务第8章. 闹钟和调度程序       掌握Android定时任务和调度作业第9章.首选项和设置       学会使用Preferences保存用户数据,编写应用的设置界面,存取设置数据第10章.使用 Room 存储数据       学会使用Android SQLite数据库,通过Room组件对数据库增删改查,掌握LiveData和 ViewModel等架构组件第11章:App上架​     1.学会App在国内应用市场上架​     2.学会App在Google play上架课程中如果讲的不对的地方,请大家指出,我及时修正,我们共同努力,一起进步。

562

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告