多線程常用的一些知識點

來源:互聯網
上載者:User

標籤:多線程

多線程常用的一些知識點:

/** * @author Rollen-Holt * 取得線程的名稱 * */class hello implements Runnable {    public void run() {        for (int i = 0; i < 3; i++) {            System.out.println(Thread.currentThread().getName());//說明如果我們沒有指定名字的話,系統自動提供名字。        }    }    public static void main(String[] args) {        hello he = new hello();        new Thread(he,"A").start();        new Thread(he,"B").start();        new Thread(he).start();    }}提醒一下大家:main 方法其實也是一個線程。在 java 中所以的線程都是同時啟動的,至於什麼時候,哪個先執行,完全看誰先得到 CPU 的資源。在java中,每次程式運行至少啟動2個線程。一個是 main 線程,一個是垃圾收集線程。因為每當使用 java 命令執行一個類的時候,實際上都會啟動一個 JVM,每一個 JVM 實習在就是在作業系統中啟動了一個進程。判斷線程是否啟動   isAlivejoin();  //強制執行該線程,直到該線程運行完成後,我們才繼續執行其他的。線程的優先順序設定public static void main(String[] args) {        Thread h1=new Thread(new hello(),"A");        Thread h2=new Thread(new hello(),"B");        Thread h3=new Thread(new hello(),"C");        h1.setPriority(8);//優先順序最高        h2.setPriority(2);//優先順序最低        h3.setPriority(6);        h1.start();        h2.start();        h3.start();    }


本文出自 “JianBo” 部落格,轉載請與作者聯絡!

多線程常用的一些知識點

聯繫我們

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