android:Background線程池和UiThread線程池

來源:互聯網
上載者:User

標籤:runnable   android   thread   ui線程   background   

由來

希望在代碼的任何地方,無論是在Ui線程中調用,還是Thread中調用,都能指定Runnable執行的所在的線程池。

Codes
package 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 http://www.mobctrl.net * */public final class GlobalExecutor implements Executor {    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());    }    @Override    public void execute(final Runnable command) {        executeInBackground(command, 0);    }    /**     * execute the command in background thread with a delay of {@link #delay}     *      * @param command     */    public void execute(final Runnable command, int delay) {        executeInBackground(command, delay);    }    /**     * execute the command in UiThread     *      * @param command     */    public void executeInUiThread(final Runnable command) {        mainHandler.post(command);    }    /**     * execute the command in main thread with a delay of {@link #delay}     *      * @param command     */    public void executeInUiThread(final Runnable command, int delay) {        mainHandler.postDelayed(command, delay);    }    /**     * execute the command in background thread with a delay of {@link #delay}     *      * @param command     */    public void executeInBackground(final Runnable command, final int delay) {        if (isOnMainThread()) {            executeDelayedAfterIdleUnsafe(command, delay);        } else {            mainHandler.post(new Runnable() {                @Override                public void run() {                    executeDelayedAfterIdleUnsafe(command, delay);                }            });        }    }    /**     * execute the command in background thread     *      * @param command     */    public void executeInBackground(final Runnable command) {        executeInBackground(command, 0);    }    private boolean isOnMainThread() {        return Looper.getMainLooper().getThread() == Thread.currentThread();    }    private void executeDelayedAfterIdleUnsafe(final Runnable task,            final int delay) {        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {            @Override            public boolean queueIdle() {                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;public class MainActivity extends Activity {    private GlobalExecutor mGlobalExecutor = null;    private Button btn1;    private Button btn2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mGlobalExecutor = new GlobalExecutor();        btn1 = (Button) findViewById(R.id.btn_1);        btn1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                System.out.println("debug:btn 1 click");                btn1.setBackgroundColor(Color.BLACK);                mGlobalExecutor.execute(new Runnable() {                    @Override                    public void run() {                        System.out.println("debug:click1 execute tid = "                                + Process.myTid() + ",pid=" + Process.myPid());                    }                });                mGlobalExecutor.executeInUiThread(new Runnable() {                    @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(new OnClickListener() {            @Override            public void onClick(View arg0) {                System.out.println("debug:btn 2 click");                new Thread(new Runnable() {                    @Override                    public void run() {                        System.out.println("debug: click2(run) tid = "                                + Process.myTid() + ",pid=" + Process.myPid());                        mGlobalExecutor.execute(new Runnable() {                            @Override                            public void run() {                                System.out                                        .println("debug: click2 run execute in thread- tid = "                                                + Process.myTid()                                                + ",pid="                                                + Process.myPid());                            }                        });                        mGlobalExecutor                                .executeInUiThread(new Runnable() {                                    @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

相繼點擊按鈕btn1和btn2,日誌如下:

05-20 10:25:49.045: I/System.out(3144): debug:btn 1 click05-20 10:25:49.115: I/System.out(3144): debug: click1 executeInUiThread tid = 3144,pid=314405-20 10:25:49.125: I/System.out(3144): debug:click1 execute tid = 3157,pid=314405-20 10:25:50.105: I/System.out(3144): debug:btn 2 click05-20 10:25:50.105: I/System.out(3144): debug: click2(run) tid = 3582,pid=314405-20 10:25:50.145: I/System.out(3144): debug: click2 run execute in UiThread- tid = 3144,pid=314405-20 10:25:50.175: I/System.out(3144): debug: click2 run execute in thread- tid = 3157,pid=3144
優點

我們可以在任意線程中調用我們想執行的Runnable,非常方便。

更多交流

Android開發聯盟QQ群:272209595

android:Background線程池和UiThread線程池

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.