目錄
1.認識多線程
1.1線程的概念
1.2進程和線程
1.2.1進程和線程用圖描述關系
?1.2.2進程和線程的區別
1.3Java 的線程和操作系統線程的關系?
?2.創建線程
2.1繼承 Thread 類
?2.2實現 Runnable 接口
?2.3匿名內部類創建 Thread 子類對象
2.4匿名內部類創建 Runnable 子類對象
2.5lambda 表達式創建 Runnable 子類對象?
1.認識多線程
1.1線程的概念
引入線程:進程雖然可以很好的實現并發編程,但在進行頻繁的進程創建和銷毀的過程中開銷比較大(體現在資源的申請和釋放上)。所以就發明了比進程更輕量的線程。
線程與進程相比:
(1)創建線程比創建進程更快 .(2)銷毀線程比銷毀進程更快 .(3)調度線程比調度進程更快 .
1.2進程和線程
1.2.1進程和線程用圖描述關系

?1.2.2進程和線程的區別
(高頻面試題)
1.進程是包含線程的。
2.每個線程就是一個?獨立的"執行流",可以單獨執行一些代碼,并參與到CPU的調度中(狀態,上下文,優先級,記賬信息,每個線程都有自己的一份)。
3.每個進程都有自己的資源,進程中的線程共用一份資源(內存空間和文件描述符表)。
(2)與(3)說明:
進程是系統分配資源的最小單位,線程是系統調度的最小單位。
4.進程與進程之間不會相會影響,但?線程與線程之間會相會影響。如果一個進程中的某個線程拋出異常,可能會導致進程中的所有線程都異常終止。
1.3Java 的線程和操作系統線程的關系?
1.線程是操作系統中的概念 . 操作系統內核實現了線程這樣的機制 , 并且對用戶層提供了一些 API 供用戶使用(例如 Linux 的 pthread 庫 ).2. Java 標準庫中 Thread 類可以視為是對操作系統提供的 API 進行了進一步的抽象和封裝 .
注意:
(1)線程與線程之間可能會相互干擾,產生邏輯bug,引起線程安全。
(2)線程不是越多越好,線程太多調度開銷可能會非常明顯。
?
?2.創建線程
2.1繼承 Thread 類
(1)繼承 Thread 來創建一個線程類
class MyThread extends Thread {
?? ?@Override
?? ?public void run() {
?? ??? ?System.out.println("這里是線程運行的代碼");
?? ?}
}
此處?run()不需要手動調動,在線程創建好之后JVM會自動調用執行(回調函數)。
(2)創建 MyThread 類的實例
MyThread t = new MyThread();
創建出的實例才是真線程。?
?(3)調用 start 方法啟動線程?
t.start(); // 線程開始運行
此時才會真正調用系統API,在系統內核中創建出線程(執行run())。
?為啥要在系統內核中創建出線程?
因為程序有時需要對軟硬件資源進行操作。
完整示例:
public class ThreadDemo1 {public static void main(String[] args) {Thread t = new MyThread();t.start();while(true){System.out.println("main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}class MyThread extends Thread{@Overridepublic void run() {while(true){System.out.println("MyThread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
?進程創建第一個線程時開銷最大,之后的線程開銷都比較小,但不是0,main線程其實是第一個線程。
?
一直循環……?
?當有多個線程,它們的執行順序是不確定的。
?使用 jconsole 命令觀察線程
我們可以使用jdk自帶的工具 jconsole查看當前Java進程中所有的線程
第一步,找到jdk
第二步,點進去,找到里面的bin文件點進去
第三步,在bin文件夾里搜索jconsole?
第四步,找到你所創建進程點擊線程進行查看
?
?2.2實現 Runnable 接口
(1) 實現 Runnable 接口
class MyRunnable implements Runnable {@Overridepublic void run () {System . out . println ( " 這里是線程運行的代碼 " );? }}
(2) 創建 Thread 類實例, 調用 Thread 的構造方法時將 Runnable 對象作為 參數.
Thread t = new Thread(new MyRunnable());
(3) 調用 start 方法
t.start(); // 線程開始運行?
?完整示例:
public class ThreadDemo2 {public static void main(String[] args) {Thread t = new Thread(new MyRunnable());t.start();while(true){System.out.println("main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}class MyRunnable implements Runnable{@Overridepublic void run() {while(true){System.out.println("MyRunnable");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
1.繼承 Thread 類 , 直接使用 this 就表示當前線程對象的引用 .2.實現 Runnable 接口 , this 表示的是 MyRunnable 的引用 . 需要使用 Thread.currentThread()
?2.3匿名內部類創建 Thread 子類對象
// 使用匿名類創建 Thread 子類對象Thread t1 = new Thread () {@Overridepublic void run () {System . out . println ( " 使用匿名類創建 Thread 子類對象 " );? }};
??完整示例:
public class ThreadDemo3 {public static void main(String[] args) {Thread t=new Thread(){@Overridepublic void run() {while (true){System.out.println("使用匿名類創建 Thread 子類對象");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};t.start();while(true){System.out.println("main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
2.4匿名內部類創建 Runnable 子類對象
// 使用匿名類創建 Runnable 子類對象Thread t2 = new Thread ( new Runnable () {@Overridepublic void run () {System . out . println ( " 使用匿名類創建 Runnable 子類對象 " );? }});
??完整示例:
public class ThreadDemo4 {public static void main(String[] args) {Thread t=new Thread(new Runnable() {@Overridepublic void run() {while(true){System.out.println("使用匿名類創建Runnable子類對象");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}});t.start();while(true){System.out.println("main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
2.5lambda 表達式創建 Runnable 子類對象?
?lambda就是讓方法看上去脫離類,單獨存在。
// 使用 lambda 表達式創建 Runnable 子類對象Thread t3 = new Thread (() -> System . out . println ( " 使用匿名類創建 Thread 子類對象 " ));Thread t4 = new Thread (() -> {System . out . println ( " 使用匿名類創建 Thread 子類對象 " );});
??完整示例:
public static void main(String[] args) {Thread t=new Thread(() -> {while(true){System.out.println("使用匿名類創建Runnable子類對象");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();while(true){System.out.println("main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}
都看到這了,不如關注一下,給個免費的贊?