【笔记】【从Android Guide温习Android 一】进程和线程(Process And thread)

时间:2014-01-14 19:37:59   收藏:0   阅读:546

 

【笔记】【从Android Guide温习Android 一】进程和线程(Process And thread)

前言

基于以上两点,打算重新整理一下Android知识。从Android Guide入手。

对于读者,非常感谢抽出时间来看我的笔记,若有什么错误请指正。有什么意见可以私信我。

虽然从官方的Android Guide开始。

但说实话。我觉得官方文档并不适合新人(作为新人,我更喜欢iOS的文档,当然也有可能我有android经验。思路会类比一下感觉很简单)。

若需要明白个8 9分的东西需要你具备一定的Android项目开发经验才行。

一切从这里开始

当我们启动应用的最开始,先创建了一个linux进程和一个主线程,根据应用需要在创建了其他进程和线程。然后所有的操作化作指令,逐一交付给线程执行。

来自Android Guide - Processes and Threads
When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread). If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution. However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process.
大致如下

组建(components)是指: activity, service, receiver, provider

进程(Process)

可以通过设置AndroidManifest.xml中的组建属性"android:process"来确定运行在其他进程。

<service android:name=".demo.service.remind.TYRemindServiceImpl"
         android:process="remindservice" /> <!-- remindservice -->
<service android:name=".demo.service.remind.TYRemindServiceImpl"
         android:process=":remindservice" /> <!-- :的作用:使得该线程名为包名:remindservice -->

生命周期

耗时工作的选择

线程(Thread)

主线程(Main Thread or UI Thread)

工作线程(Worker Thread or Background Thread)

工作线程这个名字一开始让我很困扰。以为是什么特殊的线程。其实没什么,只是相对于主线程的一种说法。

下面是例子,均完成相同的操作,工作线程下载图片,通知主线程显示图片。来自官方文档

// 错误例子
new Thread(new Runnable() {
    public void run() {
        Bitmap b = loadImageFromNetwork("http://example.com/image.png");

        mImageView.setImageBitmap(b); // 这是错误的,违反"综述"中的第4条
       }
}).start();

// 使用Activity
Activity activity = this;// 假设获取Activity实例

new Thread(new Runnable() {
    public void run() {
        Bitmap b = loadImageFromNetwork("http://example.com/image.png");

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mImageView.setImageBitmap(b); // 这是错误的,违反"综述"中的第4条
            }
        });
    }
}).start();   

// 使用Post
public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
            mImageView.post(new Runnable() {
                public void run() {
                    mImageView.setImageBitmap(bitmap);
                }
            });
        }
    }).start();
}

// 使用AsyncTask

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}

// 使用Handler
// 在主线程创建Handler实例
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        mImageView.setImageBitmap(msg.obj);
    }
};

new Thread(new Runnable() {
    public void run() {
        final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
        Message msg = handler.obtainMessage(0, bitmap);
        handler.sendMessage(msg);
    }
}).start();

线程安全

总的来说,当你使用多线程或多进程时,数据通讯是比较繁琐且易出错的工作。

但你可以使用Android的一些本地化特性,可以帮你省去很多工作,比如官网举两点的例子。

IPC(进程间通讯)

官网仅提及了Service 通过绑定进行通讯。使用Messenger.这部分以后在做介绍。

总结

原文:http://www.cnblogs.com/blog-wenfeng/p/3512587.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!