android:Background线程池和UiThread线程池

由来

希望在代码的任何地方,无论是在Ui线程中调用,还是Thread中调用,都能指定Runnable执行的所在的线程池。

Codespackage com.example.androidbackgroundexecuter;import java.util.concurrent.Executor;import android.os.Handler;import android.os.HandlerThread;import android.os.Looper;import android.os.MessageQueue;/** * * @author Zheng Haibo * @Web * */{static final String APP_EXECUTOR = “APP_EXECUTER”;private final Handler mainHandler;private final Handler backgroundHandler;public GlobalExecutor() {if (!isOnMainThread()) {throw new IllegalStateException(“Error!Please init the Executor in the main thread…”);}mainHandler = new Handler(Looper.getMainLooper());HandlerThread handlerThread = new HandlerThread(APP_EXECUTOR);handlerThread.start();backgroundHandler = new Handler(handlerThread.getLooper());}(final Runnable command) {executeInBackground(command, 0);}/*** execute the command in background thread with a delay of {@link #delay}** @param command*/(final Runnable command, int delay) {executeInBackground(command, delay);}/*** execute the command in UiThread** @param command*/(final Runnable command) {mainHandler.post(command);}/*** execute the command in main thread with a delay of {@link #delay}** @param command*/(final Runnable command, int delay) {mainHandler.postDelayed(command, delay);}/*** execute the command in background thread with a delay of {@link #delay}** @param command*/(final Runnable command, final int delay) {if (isOnMainThread()) {executeDelayedAfterIdleUnsafe(command, delay);} else {mainHandler.post(new Runnable() {() {executeDelayedAfterIdleUnsafe(command, delay);}});}}/*** execute the command in background thread** @param command*/(final Runnable command) {executeInBackground(command, 0);}() {return Looper.getMainLooper().getThread() == Thread.currentThread();}(final Runnable task,final int delay) {Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {() {backgroundHandler.postDelayed(task, delay);return false;}});}}

注意,new GlobalExecutor()是需要在主线程。

使用举例package com.example.androidbackgroundexecuter;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.os.Process;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;{private GlobalExecutor mGlobalExecutor = null;private Button btn1;private Button btn2;(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mGlobalExecutor = new GlobalExecutor();btn1 = (Button) findViewById(R.id.btn_1);btn1.setOnClickListener(new OnClickListener() {(View arg0) {System.out.println(“debug:btn 1 click”);btn1.setBackgroundColor(Color.BLACK);mGlobalExecutor.execute(new Runnable() {() {System.out.println(“debug:click1 execute tid = “+ Process.myTid() + “,pid=” + Process.myPid());}});mGlobalExecutor.executeInUiThread(new Runnable() {() {System.out.println(“debug: click1 executeInUiThread tid = “+ Process.myTid()+ “,pid=”+ Process.myPid());}});}});btn2 = (Button) findViewById(R.id.btn_2);btn2.setOnClickListener(new OnClickListener() {(View arg0) {System.out.println(“debug:btn 2 click”);new Thread(new Runnable() {() {System.out.println(“debug: click2(run) tid = “+ Process.myTid() + “,pid=” + Process.myPid());mGlobalExecutor.execute(new Runnable() {() {System.out.println(“debug: click2 run execute in thread- tid = “+ Process.myTid()+ “,pid=”+ Process.myPid());}});mGlobalExecutor.executeInUiThread(new Runnable() {() {System.out.println(“debug: click2 run execute in UiThread- tid = “+ Process.myTid()+ “,pid=”+ Process.myPid());btn1.setBackgroundColor(Color.RED);}});}}).start();}});}}Print福报不够的人,就会常常听到是非;

android:Background线程池和UiThread线程池

相关文章:

你感兴趣的文章:

标签云: