多線程-join()方法

來源:互聯網
上載者:User

標籤:art   print   運算   class   dom   extend   進程   對象   adc   

在很多情況下,主進程建立並啟動子線程,如果子線程中要進行大量的耗時運算,主線程往往將早於子線程結束之前結束。這時,如果主線程想等待子線程執行完成之後再結束,比如子線程處理一個資料,主線程要取得這個資料中的值,就要用到join()方法了。方法join()的作用是等待線程對象銷毀。

package org.github.lujiango;public class Test01 {    static class MyThread extends Thread {        @Override        public void run() {            try {                int sec = (int) (Math.random() * 10000);                System.out.println(sec);                Thread.sleep(sec);            } catch (Exception e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        try {            MyThread t = new MyThread();            t.start();            t.join();            System.out.println("我想當t對象執行完畢後我再執行...");        } catch (Exception e) {            e.printStackTrace();        }    }}

方法join的作用是使所屬的線程對象x正常執行run()方法中的任務,而使當前線程z進行無限期的阻塞,等待線程x銷毀後再繼續執行線程z後面的代碼。 
方法join具有使線程排隊啟動並執行作用,有些類似同步的運行效果。join與synchronized的區別是:join在內部使用wait()方法進行等待,而sychronized關鍵字使用的是“對象監視器”原理作為同步。 
join方法的簽名 
join的功能內部是使用wait(long)方法來實現的,所有join方法具有釋放鎖的特點:

 public final void join() throws InterruptedException{                join(0);  }

@param millis 等待時間,單位:毫秒(如果逾時,則因為另一個線程join而導致阻塞的線程繼續執行)

public final synchronized void join(long millis) throws InterruptedException {}

join與異常

在join過程中,如果當前線程對象被中斷,則當前線程出現異常。  

package org.github.lujiango;public class Test02 {    static class ThreadA extends Thread {        @Override        public void run() {            for (int i = 0; i < Integer.MAX_VALUE; i++) {                String newString = new String();                Math.random();            }        }    }    static class ThreadB extends Thread {        @Override        public void run() {            try {                ThreadA a = new ThreadA();                a.start();                a.join();                System.out.println("Thread B run end...");            } catch (Exception e) {                System.out.println("Thread B catch");                e.printStackTrace();            }        }    }    static class ThreadC extends Thread {        private ThreadB threadB;        public ThreadC(ThreadB threadB) {            this.threadB = threadB;        }        @Override        public void run() {            threadB.interrupt();        }    }    public static void main(String[] args) {        try {            ThreadB b = new ThreadB();            b.start();            Thread.sleep(500);            ThreadC c = new ThreadC(b);            c.start();        } catch (Exception e) {            e.printStackTrace();        }    }}

join與interrupt方法如果遇到,就會出現異常,但今次按鈕還是呈“紅色”,原因是線程a還在繼續運行,a為出現異常,是正常執行的狀態。

多線程-join()方法

聯繫我們

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