標籤:多線程
多線程常用的一些知識點:
/** * @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” 部落格,轉載請與作者聯絡!
多線程常用的一些知識點