欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Thread 介绍与实例

程序员文章站 2023-11-12 10:09:10
android中很重要的一个机制就是线程+消息,当然线程并不是android独有的,下面,简单的说说使用线程的时候应该注意的地方 我们采用最简单的方法来建立一个andro...

android中很重要的一个机制就是线程+消息,当然线程并不是android独有的,下面,简单的说说使用线程的时候应该注意的地方

我们采用最简单的方法来建立一个android的线程+消息的例子

1.thread + handler

[java]

复制代码 代码如下:

package com.example.test_thread;

import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.widget.textview;

public class mainactivity extends activity {

    textview mtextview = null;
//  static textview mtextview = null; 
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        mtextview = (textview)findviewbyid(r.id.textview);
        thread th = new thread(new runnable() {

            @override
            public void run() {
                // todo auto-generated method stub 
                for(int i = 0;i<1000;i++)
                {
                    try {
                        thread.sleep(500);
                        system.out.println("thread running :"+i+"!");
                        message msg = new message();
                        msg.what = i;
                        mhandler.sendmessage(msg);
                    } catch (interruptedexception e) {
                        // todo auto-generated catch block 
                        e.printstacktrace();
                    }
                }
            }
        });
        th.start();

    }
    public handler mhandler = new handler(){
//    public static handler mhandler = new handler(){ 

        @override
        public void handlemessage(message msg) {
            // todo auto-generated method stub 
            super.handlemessage(msg);

            mtextview.settext(string.valueof(msg.what));
        }

    };

}

package com.example.test_thread;

import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.widget.textview;

public class mainactivity extends activity {

 textview mtextview = null;
// static textview mtextview = null;
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        mtextview = (textview)findviewbyid(r.id.textview);
        thread th = new thread(new runnable() {

   @override
   public void run() {
    // todo auto-generated method stub
    for(int i = 0;i<1000;i++)
    {
     try {
      thread.sleep(500);
      system.out.println("thread running :"+i+"!");
      message msg = new message();
      msg.what = i;
      mhandler.sendmessage(msg);
     } catch (interruptedexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
     }
    }
   }
  });
        th.start();

    }
    public handler mhandler = new handler(){
//    public static handler mhandler = new handler(){

  @override
  public void handlemessage(message msg) {
   // todo auto-generated method stub
   super.handlemessage(msg);

   mtextview.settext(string.valueof(msg.what));
  }

    };

}

当我们用以上方式建立线程时,进入应用之后,线程开始运行,handler接收消息改变ui中的textview,此时一切正常

当按下退出时,程序退出,但是程序进程还在stack中,因此主线程之子线程,也就是我们定义的th(th_1)不会退出,此时,在log信息中可以看到,system.out还在print数字

当再次进入程序的时候,可以看到,log中打印的信息double,但是ui会按照新线程(th_2)的次序改变

此时th_1仍在运行,th_1使用的 handler_1也在运行,只不过上一个activity的状态已经是finish,因此不会改变ui  this ->mfinished= true

其实只要th_1中有关于上一个activity的引用,那么activity就不会销毁,java的机制就是这样,这是我们推荐的线程机制,下面着重说一下可能遇到的问题

2.同样是刚刚的例子,我们将handler定义成static

[java]
public static handler mhandler = new handler(){

public static handler mhandler = new handler(){此时,在退出应用再重新进入时,由于handler并不会有新的实例,因此,th_1与th_2同时发消息给一个static handler 或者说是指向了同一块内存区域,这时就会出现textview上的数字来回跳的现象

3.这样也可以

使用static定义handler也不是不可以,只要在activity的oncreate()中重新实例一个handler,这样,jvm分配另一块内存给新的handler,这样运行就正常了

[java]

复制代码 代码如下:

package com.example.test_thread;

import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.widget.textview;

public class mainactivity extends activity {

    public handler mhandler = null;
    textview mtextview = null;
//  static textview mtextview = null; 
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        mtextview = (textview)findviewbyid(r.id.textview);
        mhandler = new testhandler();
        thread th = new thread(new runnable() {

            @override
            public void run() {
                // todo auto-generated method stub 
                for(int i = 0;i<1000;i++)
                {
                    try {
                        thread.sleep(500);
                        system.out.println("thread running :"+i+"!");
                        message msg = new message();
                        msg.what = i;
                        mhandler.sendmessage(msg);
                    } catch (interruptedexception e) {
                        // todo auto-generated catch block 
                        e.printstacktrace();
                    }
                }
            }
        });
        th.start();

    }
    class testhandler extends handler
    {
        @override
        public void handlemessage(message msg) {
            // todo auto-generated method stub 
            super.handlemessage(msg);
            system.out.println("handler running :"+msg.what+"!");
            mtextview.settext(string.valueof(msg.what));
        }

    }
}

package com.example.test_thread;

import android.app.activity;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.widget.textview;

public class mainactivity extends activity {

    public handler mhandler = null;
 textview mtextview = null;
// static textview mtextview = null;
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        mtextview = (textview)findviewbyid(r.id.textview);
        mhandler = new testhandler();
        thread th = new thread(new runnable() {

   @override
   public void run() {
    // todo auto-generated method stub
    for(int i = 0;i<1000;i++)
    {
     try {
      thread.sleep(500);
      system.out.println("thread running :"+i+"!");
      message msg = new message();
      msg.what = i;
      mhandler.sendmessage(msg);
     } catch (interruptedexception e) {
      // todo auto-generated catch block
      e.printstacktrace();
     }
    }
   }
  });
        th.start();

    }
    class testhandler extends handler
    {
  @override
  public void handlemessage(message msg) {
   // todo auto-generated method stub
   super.handlemessage(msg);
   system.out.println("handler running :"+msg.what+"!");
   mtextview.settext(string.valueof(msg.what));
  }

    }
}


当然,总的来说java还是不推荐使用static变量的,这本身也不符合面向对象的变成思想,所以,建议除了一些final值,尽量还是多使用消息机制来解决问题,维护也轻松些