Android开发 Handler Runnable和Thread之间的区别和联系 应用----------------

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口;Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限。

下面看例子:

 
  package org.thread.demo;   
   
  class 
   MyThread  
  extends 
   Thread{   
   
  private 
   String name;   
   
  public 
   MyThread(String name) {   
   
  super 
  ();   
   
  this 
  .name = name;   
  }   
   
  public 
    
  void 
   run(){   
   
  for 
  ( 
  int 
   i= 
  0 
  ;i< 
  10 
  ;i++){   
  System.out.println( 
  "线程开始:" 
  + 
  this 
  .name+ 
  ",i=" 
  +i);   
  }   
  }   
  }   
   
  package 
   org.thread.demo;   
   
  public 
    
  class 
   ThreadDemo01 {   
   
  public 
    
  static 
    
  void 
   main(String[] args) {   
  MyThread mt1= 
  new 
   MyThread( 
  "线程a" 
  );   
  MyThread mt2= 
  new 
   MyThread( 
  "线程b" 
  );   
  mt1.run();   
  mt2.run();   
  }   
  }  
  

但是,此时结果很有规律,先第一个对象执行,然后第二个对象执行,并没有相互运行。在JDK的文档中可以发现,一旦调用start()方法,则会通过JVM找到run()方法。下面启动start()方法启动线程:

 
  
  1. package org.thread.demo;  
  2. public class ThreadDemo01 {  
  3. public static void main(String[] args) {  
  4. MyThread mt1=new MyThread("线程a");  
  5. MyThread mt2=new MyThread("线程b");  
  6. mt1.start();  
  7. mt2.start();  
  8. }  
  9. }; 

这样程序可以正常完成交互式运行。那么为啥非要使用start();方法启动多线程呢?

在JDK的安装路径下,src.zip是全部的java源程序,通过此代码找到Thread中的start()方法的定义,可以发现此方法中使用了private native void start0();其中native关键字表示可以调用操作系统的底层函数,那么这样的技术成为JNI技术(java Native Interface)

Runnable接口

在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。

 
  
  1. public interface Runnable{  
  2. public void run();  

例子:

 
  
  1. package org.runnable.demo;  
  2. class MyThread implements Runnable{  
  3. private String name;  
  4. public MyThread(String name) {  
  5. this.name = name;  
  6. }
  7. public void run(){  
  8. for(int i=0;i<100;i++){  
  9. System.out.println("线程开始:"+this.name+",i="+i);  
  10. }  
  11. }  
  12. }; 

但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer)此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。(start()可以协调系统的资源):

 
  
  1. package org.runnable.demo;  
  2. import org.runnable.demo.MyThread;  
  3. public class ThreadDemo01 {  
  4. public static void main(String[] args) {  
  5. MyThread mt1=new MyThread("线程a");  
  6. MyThread mt2=new MyThread("线程b");  
  7. new Thread(mt1).start();  
  8. new Thread(mt2).start();  
  9. }  

两种实现方式的区别和联系:

在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:

  • 避免点继承的局限,一个类可以继承多个接口。
  • 适合于资源的共享

以卖票程序为例,通过Thread类完成:

 
  
  1. package org.demo.dff;  
  2. class MyThread extends Thread{  
  3. private int ticket=10;  
  4. public void run(){  
  5. for(int i=0;i<20;i++){  
  6. if(this.ticket>0){  
  7. System.out.println("卖票:ticket"+this.ticket--);  
  8. }  
  9. }  
  10. }  
  11. }; 

下面通过三个线程对象,同时卖票:

 
  
  1. package org.demo.dff;  
  2. public class ThreadTicket {  
  3. public static void main(String[] args) {  
  4. MyThread mt1=new MyThread();  
  5. MyThread mt2=new MyThread();  
  6. MyThread mt3=new MyThread();  
  7. mt1.start();//每个线程都各卖了10张,共卖了30张票  
  8. mt2.start();//但实际只有10张票,每个线程都卖自己的票  
  9. mt3.start();//没有达到资源共享  
  10. }  

如果用Runnable就可以实现资源共享,下面看例子:

 
  
  1. package org.demo.runnable;  
  2. class MyThread implements Runnable{  
  3. private int ticket=10;  
  4. public void run(){  
  5. for(int i=0;i<20;i++){  
  6. if(this.ticket>0){  
  7. System.out.println("卖票:ticket"+this.ticket--);  
  8. }  
  9. }  
  10. }  
  11. }  
  12. package org.demo.runnable;  
  13. public class RunnableTicket {  
  14. public static void main(String[] args) {  
  15. MyThread mt=new MyThread();  
  16. new Thread(mt).start();//同一个mt,但是在Thread中就不可以,如果用同一  
  17. new Thread(mt).start();//个实例化对象mt,就会出现异常  
  18. new Thread(mt).start();  
  19. }  
  20. }; 

虽然现在程序中有三个线程,但是一共卖了10张票,也就是说使用Runnable实现多线程可以达到资源共享目的。

Runnable接口和Thread之间的联系:

public class Thread extends Object implements Runnable

发现Thread类也是Runnable接口的子类。


第二:

Thread是系统给你的资源,有了Thread你才有从CPU那里得到可执行时间片的权力, Thread并不认识你的程序,不知道有test 这样的类,因为编序员有千千万,每个人命名都不一样,想要做的事都不一样, 所以 Thread只认识一个! 那就是Runnable 。 Thread认识Runnable 并且知道Runnable 里面有一个run方法. 一旦调用Thread的start方法,Runnable 方法里的run就会被Thread自动运行。 所以,当我们把我们的类继承(这里应该叫实现接口)自Runnable 的时候,我们的程序就是属于Runnable 一个类型的了。 虽然是Runnable 的子类,但人家认识你爸爸,当然也知道了你。 Thread可以不管你内部有什么情况,他只管你有run()方法就行了,他就调start让你去运行run 所以我们在run里面写点东西,这样就可以让系统运行我们想要做的代码了。 是不是很通俗很易懂呢? 所以要运行线程的步骤是, 1。生成我们自己的类对象 2。从系统那里得到Thread 3。让Threa调我们的类对象,让其start起来 代码: test a=new test(); Thread thread=new Thread(a); //Thread需要一个参数,就是你编的线程类,这样他就认识了你的线程,也有资格向系统申请拿到CPU时间片thread.start(); 你可以简单点写: new Thread(a).start();

第三:

Runnable 并不一定是新开一个线程,比如下面的调用方法就是运行在UI主线程中的:

     Handler mHandler=new Handler();      mHandler.post(new Runnable(){         @Override public void run()         { // TODO Auto-generated method stub          }      });

官方对这个方法的解释如下,注意其中的:“The runnable will be run on the user interface thread.

boolean android.view.View .post(Runnable action)

Causes the Runnable to be added to the message queue. The runnable will be run on the user interface thread.

Parameters:

action The Runnable that will be executed.

Returns:

Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.

我们可以通过调用handler的post方法,把Runnable对象(一般是Runnable的子类)传过去;handler会在looper中调用这个Runnable的Run方法执行。

Runnable是一个接口,不是一个线程,一般线程会实现Runnable。所以如果我们使用匿名内部类是运行在UI主线程的,如果我们使用实现这个Runnable接口的线程类,则是运行在对应线程的。

具体来说,这个函数的工作原理如下:

View.post(Runnable)方法。在post(Runnable action)方法里,View获得当前线程(即UI线程)的Handler,然后将action对象post到Handler里。在Handler里,它将传递过来的action对象包装成一个Message(Message的callback为action),然后将其投入UI线程的消息循环中。在Handler再次处理该Message时,有一条分支(未解释的那条)就是为它所设,直接调用runnable的run方法。而此时,已经路由到UI线程里,因此,我们可以毫无顾虑的来更新UI。

如下图,前面看到的代码,我们这里Message的callback为一个Runnable的匿名内部类

这种情况下,由于不是在新的线程中使用,所以千万别做复杂的计算逻辑。

image



第四:在多线程编程这块,我们经常要使用Handler,Thread和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢?

  首先说明Android的CPU分配的最小单元是线程,Handler一般是在某个线程里创建的,因而Handler和Thread就是相互绑定的,一一对应。

  而Runnable是一个接口,Thread是Runnable的子类。所以说,他俩都算一个进程。

  HandlerThread顾名思义就是可以处理消息循环的线程,他是一个拥有Looper的线程,可以处理消息循环。

  与其说Handler和一个线程绑定,不如说Handler是和Looper一一对应的。

  最后需要说明的是,在UI线程(主线程)中:

  mHandler=new Handler();

  mHandler.post(new Runnable(){

  void run(){

  //执行代码...

  }

  });

  这个线程其实是在UI线程之内运行的,并没有新建线程。

  常见的新建线程的方法是:

  Thread thread = new Thread();

  thread.start();

  HandlerThread thread = new HandlerThread("string");

  thread.start();

第五: Java Runnable接口在进行相关编写的时候需要我们不断的学习相关代码。下面我们就来看炫如何才能使用相关的代码。Runnable接口只有一个方法run(),我们声明自己的类实现Runnable接 口并提供这一方法,将我们的线程代码写入其中,就完成了这一部分的任务。

  但是Runnable接口并没有任何对线程的支持,我们还必须创建Thread类 的实例,这一点通过Thread类的构造函数public Thread(Runnable target);来实现。下面是一个例子:

  1.public class MyThread implements Runnable

  2.{

  3.int count= 1, number;

  4.public MyThread(int num)

  5.{

  6.numnumber = num;

  7.System.out.println("创建线程 " + number);

  8.}

  9.public void run()

  10.{

  11.while(true)

  12.{

  13.System.out.println

  14.("线程 " + number + ":计数 " + count);

  15.if(++count== 6) return;

  16.}

  17.}

  18.public static void main(String args[])

  19.{

  20.for(int i = 0; i 〈 5;

  21.i++) new Thread(new MyThread(i+1)).start();

  22.}

  23.}

  严格地说,创建Thread子类的实例也是可行的,但是必须注意的是,该子类必须没有覆盖 Thread 类的 run 方法,否则该线程执行的将是子类的 run 方法,而不是我们用以实现Runnable 接口的类的 run 方法,对此大家不妨试验一下。

  使用 Java Runnable接口来实现多线程使得我们能够在一个类中包容所有的代码,有利于封装,它的缺点在于,我们只能使用一套代码,若想创建多个线程并使各个线程执行不同的代 码,则仍必须额外创建类,如果这样的话,在大多数情况下也许还不如直接用多个类分别继承 Thread 来得紧凑。

参考:

http://www.cnblogs.com/qingblog/archive/2012/08/08/2628245.html

http://java.chinaitlab.com/net/807900.html

http://czhjchina.blog.163.com/blog/static/20027904720111153382495/

http://zhidao.baidu.com/question/82198667.html

http://developer.51cto.com/art/201203/321042.htm




           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hgfygfc/article/details/83916701