[Js-Java SE]線程與進程
阿新 • • 發佈:2018-03-08
pro 新的 sta pac 時間片 image spa play 單位
5.線程的創建和啟動
1 /* 2 在Java語言中實現多線程的第一種方式: 3 第一步:繼承java.lang.Thread 4 第二步:重寫run方法 5 6 三個重要知識點: 7 如何定義線程? 8 如何創建線程? 9 如何啟動線程? 10 11 */ 12 class ThreadTest02 13 { 14 public static void main(String[] args) 15 { 16 // 創建線程 17 Thread t = newProcessor(); 18 // 啟動線程 19 t.start(); // 這段代碼執行瞬間結束。告訴JVM分配一個新的棧給t線程 20 // run不需要程序員手動調用,系統線程啟動之後自動調用run方法 21 // 直接調用run方法會變成直接方法調用,程序只有一個主線程 22 23 // 這段代碼在主線程中運行 24 for (int i = 0; i < 100; i++) { 25 System.out.println("main==>" + i);26 } 27 28 // 有了多線程之後,main方法結束只是主線程棧中沒有方法棧幀了 29 // 但是其他線程或者其他棧中還有棧幀 30 // main方法結束,程序可能還在運行 31 } 32 } 33 34 // 定義一個線程 35 class Processor extends Thread 36 { 37 // 重寫run方法 38 public void run(){ 39 for (int i = 0; i < 100; i++) { 40 System.out.println("run==>" + i);41 } 42 } 43 }
1 /* 2 在Java語言中實現多線程的第二種方式: 3 第一步:實現java.lang.Runnable接口 4 第二步:重寫run方法 5 6 三個重要知識點: 7 如何定義線程? 8 如何創建線程? 9 如何啟動線程? 10 11 */ 12 class ThreadTest03 13 { 14 public static void main(String[] args) 15 { 16 //創建線程 17 Thread t = new Thread(new Processor()); 18 19 //啟動線程 20 t.start(); 21 } 22 23 } 24 25 // 這種方式是推薦的。因為一個類實現接口之外保留了類的繼承 26 class Processor implements Runnable 27 { 28 public void run(){ 29 for (int i = 0; i < 10; i++) { 30 System.out.println("run==>" + i); 31 } 32 } 33 }
6.線程的生命周期
新建:采用new語句創建完成
就緒:執行start之後
運行:占用CPU時間
阻塞:執行了wait語句、執行了sleep語句和等待某個對象鎖,等待輸入的場合
終止: 退出run方法
7.線程的調度與控制
通常我們的計算機只有一個CPU,CPU在某一個時刻只能執行一條指令,喜愛昵稱只有得到CPU時間片,也就是使用權才可以執行指令,在單CPU的機器上線程不是並行運行的,只有在多個CPU上線程才可以並行運行,Java虛擬機要負責線程的調度,取得CPU的使用權,目前有兩種調度模型:分時調度模型和搶占式調度模型。Java使用搶占式調度模型
分時調度模型:所有線程輪流使用CPU的使用權,平均分配每個線程占用CPU的時間片
搶占式調度模型:優先讓優先級高的線程使用CPU,如果線程的優先級相同,那麽會隨機選擇一個。優先級高的線程獲取的CPU時間片相對多一些
8.線程優先級
線程的優先級主要分為三種:MAX_PRIORITY(最高級),MIN_PRIORITY(最低級),NORM_PRIORITY(默認標準級別)
1 /* 2 * 線程優先級高的獲取的CPU時間片相對多一些 3 * 4 * 優先級:1~10 5 * 最低:1 6 * 最高:10 7 * 默認:5 8 */ 9 public class ThreadTest05 { 10 11 public static void main(String[] args) { 12 System.out.println(Thread.MAX_PRIORITY); //10 13 System.out.println(Thread.MIN_PRIORITY); //1 14 System.out.println(Thread.NORM_PRIORITY); //5 15 16 Thread t1 = new Processor(); 17 t1.setName("t1"); 18 19 Thread t2 = new Processor(); 20 t2.setName("t2"); 21 22 System.out.println(t1.getPriority()); //5 23 System.out.println(t2.getPriority()); //5 24 25 //設置優先級 26 t1.setPriority(4); 27 t2.setPriority(6); 28 29 //啟動線程 30 t1.start(); 31 t2.start(); 32 } 33 34 } 35 36 class Processor extends Thread { 37 38 @Override 39 public void run() { 40 for (int i = 0; i < 50; i++) { 41 System.out.println(Thread.currentThread().getName() + "-->" + i); 42 } 43 } 44 45 }
9.關於sleep方法需要註意的幾點
· sleep方法的參數單位為毫秒
· sleep方法為靜態方法,調用的時候睡眠的是調用了這個方法的線程
· 可以使用interrupt方法進行中斷,但是interrupt是成員方法(這種方法依靠的是異常處理機制)
sleep的終止:
1 package com.neu.core; 2 3 /* 4 * 打斷線程的第一種方案:利用interrupt方法觸發異常處理機制 5 */ 6 public class ThreadTest08 { 7 public static void main(String[] args) throws InterruptedException { 8 9 // 創建線程 10 Thread t = new Thread(new Processor()); 11 t.setName("t"); 12 13 // 啟動線程 14 t.start(); 15 16 // 5秒之後 17 // t.sleep(5000);起到的效果和下面一樣,因為sleep方法是靜態的 18 Thread.sleep(5000); 19 20 // 打斷t線程的休眠 21 t.interrupt(); 22 23 } 24 } 25 26 class Processor implements Runnable { 27 28 @Override 29 public void run() { 30 try { 31 Thread.sleep(5000000000L); 32 } catch (InterruptedException e) { 33 e.printStackTrace(); 34 } 35 36 for (int i = 0; i < 5; i++) { 37 System.out.println(Thread.currentThread().getName() + "-->" + i); 38 } 39 } 40 41 } 42 /* 運行結果: 43 * ---------- java ---------- 44 java.lang.InterruptedException: sleep interrupted 45 at java.lang.Thread.sleep(Native Method) 46 at Processor.run(ThreadTest08.java:29) 47 at java.lang.Thread.run(Thread.java:748) 48 t-->0 49 t-->1 50 t-->2 51 t-->3 52 t-->4 53 54 Output completed (5 sec consumed) - Normal Termination 55 * 56 * */
1 package com.neu; 2 3 /** 4 * 打斷線程的第一種方案:設置標誌符 5 */ 6 public class ThreadTest09 { 7 public static void main(String[] args) throws InterruptedException { 8 Processor processor = new Processor(); 9 Thread t = new Thread(processor); 10 11 t.setName("t"); 12 13 t.start(); 14 15 Thread.sleep(5000); 16 17 processor.run = false; 18 } 19 } 20 21 class Processor implements Runnable { 22 23 boolean run = true; 24 25 @Override 26 public void run() { 27 for (int i = 0; i < 10; i++) { 28 if (run) { 29 try { 30 Thread.sleep(1000); 31 } catch (InterruptedException e) { 32 // e.printStackTrace(); 33 } 34 System.out.println(Thread.currentThread().getName() + "-->" + i); 35 } 36 } 37 } 38 39 } 40 /** 41 * 運行結果: 42 * 43 * t-->0 44 * t-->1 45 * t-->2 46 * t-->3 47 * t-->4 48 * 49 * Process finished with exit code 0 50 */
[Js-Java SE]線程與進程