一、基本概念
Android中的多线程主要是为了操作一些耗时的工作,防止其将主程序进行阻塞,与Java一样两种方式:1、继承Thread 2、实现Runnable接。但是Android与Java的多线程有一点区别是Android中的更新UI必须在主线程中进行,否则抛出异常,因为Android的UI是线程不安全的。
1、Thread类
Android中的Thread类是扩展了java.lang.Thread类的多线程类,其中有一个构造函数其实也实现了Runnable的接口,源码如下:
/**
* Constructs a new {@code Thread} with a {@code Runnable} object, the given
* name and belonging to the {@code ThreadGroup} passed as parameter.
*
* @param group
* {@code ThreadGroup} to which the new {@code Thread} will
* belong
* @param runnable
* a {@code Runnable} whose method <code>run</code> will be
* executed by the new {@code Thread}
* @param threadName
* the name for the {@code Thread} being created
* @param stackS