Android多線程研究(1)——線程基礎及源碼剖析

來源:互聯網
上載者:User

從今天起我們來看一下Android中的多線程的知識,Android入門容易,但是要完成一個完善的產品卻不容易,讓我們從線程開始一步步深入Android內部。

一、線程基礎回顧

package com.maso.test;public class TraditionalThread {public static void main(String[] args) {/* * 線程的第一種建立方式 */Thread thread1 = new Thread(){@Overridepublic void run() {try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while(true){System.out.println(Thread.currentThread().getName());}}};thread1.start();/* *線程的第二種建立方式  */Thread thread2 = new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while (true) {System.out.println(Thread.currentThread().getName());}}});thread2.start();/* * 線程的調用優先順序 */new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while(true){System.out.println("Runnable");}}}){public void run() {try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while(true){System.out.println("Thread");}};}.start();}}
上面代碼中是我們都很熟悉的線程的兩種建立方式,如果對這些還感到陌生請先看Java線程基礎。


開啟Thread類的源碼可以看到Thread類有8個建構函式,我們先看看上面的兩種建構函式的源碼。

    public Thread() {        init(null, null, "Thread-" + nextThreadNum(), 0);    }
在構造的時候直接調用了init方法

    private void init(ThreadGroup g, Runnable target, String name,                      long stackSize) {        if (name == null) {            throw new NullPointerException("name cannot be null");        }        Thread parent = currentThread();        SecurityManager security = System.getSecurityManager();        if (g == null) {            /* Determine if it's an applet or not */            /* If there is a security manager, ask the security manager               what to do. */            if (security != null) {                g = security.getThreadGroup();            }            /* If the security doesn't have a strong opinion of the matter               use the parent thread group. */            if (g == null) {                g = parent.getThreadGroup();            }        }        /* checkAccess regardless of whether or not threadgroup is           explicitly passed in. */        g.checkAccess();        /*         * Do we have the required permissions?         */        if (security != null) {            if (isCCLOverridden(getClass())) {                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);            }        }        g.addUnstarted();        this.group = g;        this.daemon = parent.isDaemon();        this.priority = parent.getPriority();        this.name = name.toCharArray();        if (security == null || isCCLOverridden(parent.getClass()))            this.contextClassLoader = parent.getContextClassLoader();        else            this.contextClassLoader = parent.contextClassLoader;        this.inheritedAccessControlContext = AccessController.getContext();        this.target = target;        setPriority(priority);        if (parent.inheritableThreadLocals != null)            this.inheritableThreadLocals =                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);        /* Stash the specified stack size in case the VM cares */        this.stackSize = stackSize;        /* Set thread ID */        tid = nextThreadID();    }
裡面的東西比較多,但是我們可以看到會初始化一個變數Runnable  target;

下面我們再來看看run方法中是個什麼東東?

    @Override    public void run() {        if (target != null) {            target.run();        }    }
原來run方法中會先判斷是否初始化了Runnable target變數,如果沒有則空實現,如果target不為空白則先執行Runnable介面中的run方法。有的朋友可能會猜想下面的代碼會先調用Runnable介面中的run方法,然後才調用Thread實作類別中的run方法。

/* * 線程的調用優先順序 */new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while(true){System.out.println("Runnable");}}}){public void run() {try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}while(true){System.out.println("Thread");}};}.start();
其實事實並非如此,因為上面代碼中是一個匿名內部類,實際上是一種從Thread的繼承和實現,所以下面的run方法覆蓋了Thread中的run方法,所以Runnable中的run方法根本不會執行。

下面再看看Runnable介面的原始碼

publicinterface Runnable {    /**     * When an object implementing interface <code>Runnable</code> is used     * to create a thread, starting the thread causes the object's     * <code>run</code> method to be called in that separately executing     * thread.     * <p>     * The general contract of the method <code>run</code> is that it may     * take any action whatsoever.     *     * @see     java.lang.Thread#run()     */    public abstract void run();}
發現Runnable介面只有一個抽象的run方法。

為什麼要搞一個Runnable介面來實現多線程呢?從Thread繼承不是更方便嗎?Runnable介面有如下優勢,所以我們常常會選擇實現Runnable介面:

1、適合多個程式碼的線程去處理同一個資源。

public class ThreadTest1 extends Thread {private int count = 5;     public void run() {        for (int i = 0; i < 7; i++) {            if (count > 0) {                System.out.println("count= " + count--);            }        }    }     public static void main(String[] args) {    //這樣實際上是建立了三個互不影響的線程執行個體        ThreadTest1 t1 = new ThreadTest1();        ThreadTest1 t2 = new ThreadTest1();        ThreadTest1 t3 = new ThreadTest1();        t1.start();        t2.start();        t3.start();    }}
public class ThreadTest1{         public static void main(String [] args) {        MyThread my = new MyThread();        //開啟了三個線程,但是操作的是同一個run方法        new Thread(my, "1號視窗").start();        new Thread(my, "2號視窗").start();        new Thread(my, "3號視窗").start();    } }class MyThread implements Runnable{     private int ticket = 5;  //5張票     public void run() {        for (int i=0; i<=20; i++) {            if (this.ticket > 0) {                System.out.println(Thread.currentThread().getName()+ "正在賣票"+this.ticket--);            }        }    }}

2、避免Java特性中的單根繼承的限制。

3、可以保持代碼和資料的分離(建立線程數和資料無關)。

4、更能體現Java物件導向的設計特點。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.