Origin
You want to specify the thread pool where runnable executes, whether it is called in the UI thread or thread, anywhere in the code.
Codes
PackageCom.example.androidbackgroundexecuter;ImportJava.util.concurrent.Executor;ImportAndroid.os.Handler;ImportAndroid.os.HandlerThread;ImportAndroid.os.Looper;ImportAndroid.os.MessageQueue;/** * * @author Zheng haibo * @Web http://www.mobctrl.net * */ Public Final class globalexecutor implements Executor { Static FinalString App_executor ="App_executer";Private FinalHandler MainHandler;Private FinalHandler Backgroundhandler; Public Globalexecutor() {if(!isonmainthread ()) {Throw NewIllegalStateException ("error! Please init the Executor in the main thread ... "); } MainHandler =NewHandler (Looper.getmainlooper ()); Handlerthread Handlerthread =NewHandlerthread (App_executor); Handlerthread.start (); Backgroundhandler =NewHandler (Handlerthread.getlooper ()); }@Override Public void Execute(FinalRunnable command) {executeinbackground (command,0); }/** * Execute the command in background thread with a delay of {@link #delay} * * @param command */ Public void Execute(FinalRunnable command,intDelay) {executeinbackground (command, delay); }/** * Execute the command in Uithread * * @param Command */ Public void Executeinuithread(FinalRunnable command) {mainhandler.post (command); }/** * Execute the command in main thread with a delay of {@link #delay} * * @param Command * / Public void Executeinuithread(FinalRunnable command,intDelay) {mainhandler.postdelayed (command, delay); }/** * Execute the command in background thread with a delay of {@link #delay} * * @param command */ Public void Executeinbackground(FinalRunnable command,Final intDelay) {if(Isonmainthread ()) {executedelayedafteridleunsafe (command, delay); }Else{Mainhandler.post (NewRunnable () {@Override Public void Run() {executedelayedafteridleunsafe (command, delay); } }); } }/** * Execute the command in background thread * * @param Command */ Public void Executeinbackground(FinalRunnable command) {executeinbackground (command,0); }Private Boolean Isonmainthread() {returnLooper.getmainlooper (). GetThread () = = Thread.CurrentThread (); }Private void Executedelayedafteridleunsafe(FinalRunnable task,Final intDelay) {Looper.myqueue (). Addidlehandler (NewMessagequeue.idlehandler () {@Override Public Boolean Queueidle() {backgroundhandler.postdelayed (task, delay);return false; } }); }}
Note that the new Globalexecutor () is required in the main thread.
Examples of Use
PackageCom.example.androidbackgroundexecuter;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;Importandroid.os.Process;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public class mainactivity extends Activity { PrivateGlobalexecutor Mglobalexecutor =NULL;PrivateButton btn1;PrivateButton btn2;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mglobalexecutor =NewGlobalexecutor (); BTN1 = (Button) Findviewbyid (r.id.btn_1); Btn1.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View arg0) {System.out.println ("DEBUG:BTN 1 click"); Btn1.setbackgroundcolor (Color.Black); Mglobalexecutor.execute (NewRunnable () {@Override Public void Run() {System.out.println ("Debug:click1 Execute tid ="+ Process.mytid () +", pid="+ process.mypid ()); } }); Mglobalexecutor.executeinuithread (NewRunnable () {@Override Public void Run() {System.out. println ("Debug:click1 executeinuithread tid ="+ Process.mytid () +", pid="+ process.mypid ()); } }); } }); BTN2 = (Button) Findviewbyid (r.id.btn_2); Btn2.setonclicklistener (NewOnclicklistener () {@Override Public void OnClick(View arg0) {System.out.println ("DEBUG:BTN 2 Click");NewThread (NewRunnable () {@Override Public void Run() {System.out.println ("Debug:click2 (run) tid ="+ Process.mytid () +", pid="+ process.mypid ()); Mglobalexecutor.execute (NewRunnable () {@Override Public void Run() {System.out. println ("Debug:click2 run execute in thread-tid ="+ Process.mytid () +", pid="+ process.mypid ()); } }); Mglobalexecutor. Executeinuithread (NewRunnable () {@Override Public void Run() {System.out. println ("Debug:click2 run execute in uithread-tid ="+ Process.mytid () +", pid="+ process.mypid ()); Btn1.setbackgroundcolor (color.red); } }); }}). Start (); } }); }}
Print
Click the button btn1 and btn2 successively, the log is as follows:
05- - Ten: -:49.045: I/system. out(3144):Debug: BTN1Click05- - Ten: -:49.115: I/system. out(3144):Debug: Click1 Executeinuithread tid =3144, pid=314405- - Ten: -:49.125: I/system. out(3144):Debug: Click1 Execute tid =3157, pid=314405- - Ten: -:50.105: I/system. out(3144):Debug: BTN2Click05- - Ten: -:50.105: I/system. out(3144):Debug: Click2 (run) tid =3582, pid=314405- - Ten: -:50.145: I/system. out(3144):Debug: Click2 Run ExecuteinchUithread-tid =3144, pid=314405- - Ten: -:50.175: I/system. out(3144):Debug: Click2 Run ExecuteinchThread-tid =3157, pid=3144
Advantages
It is very convenient for us to invoke the runnable we want to perform in any thread.
More communication
Android Development Alliance QQ Group: 272209595
Android:background thread pool and uithread thread pool