http://android.jobbole.com/82440/
进程优先级:Active->Visible->Service->Backgroud->Empty
线程分类--
1.default group UI线程属于这类
2.backgroud group 工作线程属于这一类
Thread,AsynTask:适合单个任务的场景。
HandlerThread:适合串行处理多任务的场景,只有一个线程,所产生的线程会一直存活。(多张图片上传的时候可以使用)
<pre>
public class LoadingActivity extends Activity{
private HandlerThread handlerThread;
private Handler mHandler;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
tv = (TextView)findViewById(R.id.tv);
init();
}
private void init(){
//自带Loop
handlerThread = new HandlerThread("Main");
handlerThread.setPriority(1);
handlerThread.start();
mHandler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
tv.setText("你好啊");
return true;
}
});
mHandler.post(new Runnable() {
@Override
public void run() {
System.out.println("handler_post_cur_id="+Thread.currentThread().getId());
mHandler.sendEmptyMessage(1);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//需要手动关闭,因为Loop是无限循环的
handlerThread.quit();
}
}
</pre>
ThreadPoolExecutor:适合并行多任务的场景。http://www.jianshu.com/p/3da543063b8c
官方定义的四种线程池,都是通过Executors的工厂方法来实现
1.FixedThreadPool 一种数量固定的线程池,且任务队列没有大小限制。只有核心线程,没有超时限制。
2.CachedThreadPool 无界线程池,可以自行进行线程回收。只有非核心线程,最大线程数为Integer.MAX_VALUE。
3.SingleThreadExecutor 只有一个核心线程,所有任务都在同一线程中按序执行。
4.ScheduledThreadPool 核心线程数量固定,非核心线程没有限制。非核心线程空闲会被回收。
<pre>
public class LoadingActivity extends Activity{
private final int CORE_POOL_SIZE = 1;//核心线程数
private final int MAX_POOL_SIZE = 3;//最大线程数,线程池中的线程数不会超过最大线程数
private final int BLOCK_SIZE = 2;//阻塞队列大小,缓存队列
private final long KEEP_ALIVE_TIME = 2;//空闲线程超时时间,超时会被回收
private ThreadPoolExecutor executorPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
init();
}
private void init(){
/**
* TimeUnit.SECONDS keepAliveTime时间单位,这边设置为秒
* Executors.defaultThreadFactory()线程工厂,为线程池提供创建线程的方法
* new ThreadPoolExecutor.AbortPolicy() 拒绝任务的处理策略
* 1、ThreadPoolExecutor.AbortPolicy() 直接抛出异常RejectedExecutionException
2、ThreadPoolExecutor.CallerRunsPolicy() 直接调用run方法并且阻塞执行
3、ThreadPoolExecutor.DiscardPolicy() 直接丢弃后来的任务
4、ThreadPoolExecutor.DiscardOldestPolicy() 丢弃在队列中队首的任务
*/
executorPool = new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(BLOCK_SIZE),Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
executorPool.allowCoreThreadTimeOut(true);//设置true会超时退出,默认不退出
executorPool.execute(new WorkerThread());
}
public class WorkerThread implements Runnable {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
</pre>
</p>
IntentService:内部也是用HandlerThread的方式,适合需要在工作线程中处理UI无关任务的场景。可以做耗时操作的Service,单独处理串行任务。记得在manifest中需要配置,跟The same as Service.
<pre>
public class ConsumeService extends IntentService{
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public ConsumeService(String name) {
super(name);
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart"+startId);
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
//Intent是从Activity发过来的,携带识别参数
try {
mHandler.sendEmptyMessageAtTime(1,1000);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
</pre>