文章目錄 進程和線程 創建線程的五種寫法 繼承Thread,重寫run 實現Runnable(接口),重寫run 繼承Thread,重寫run,但是使用匿名內部類 實現Runnable(接口),重寫run,但是使用匿名內部類 使用lambda表達式 請說明Thread類中run和start的區別
進程和線程
進程
進程:是正在執行的程序,是資源分配的基本單位,具有獨立的地址空間 操作系統會為其分配CPU和內存
線程
線程:引入線程是為了解決進程開銷大,浪費資源的情景,并且多進程并發效率比較低 線程是調度執行的基本單位 線程之間會相互影響,一個線程掛了,會影響到整個進程都異常結束,線程也自然會結束
進程和線程的區別
進程包含線程,一個進程里面有多個線程或者是一個線程 進程和線程都是用來實現并發編程場景 的,但是線程比進程更輕量和高效 同一個進程的線程之間共用同一份資源(內存和硬盤),省去了申請資源的開銷 進程和進程之間都是獨立存在的,不會相互影響,同一個進程中,線程和線程之間會相互影響(線程安全問題 + 線程出現異常) 進程是分配資源的基本單位,線程是調度執行的基本單位
創建線程的五種寫法
繼承Thread,重寫run
package Thread ; class MyThread extends Thread { public void run ( ) { while ( true ) { System . out. println ( "hello Thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } }
}
public class Demo1 { public static void main ( String [ ] args) throws InterruptedException { Thread thread = new MyThread ( ) ; thread. start ( ) ; while ( true ) { System . out. println ( "hello main!" ) ; Thread . sleep ( 1000 ) ; } }
}
實現Runnable(接口),重寫run
package Thread ; class MyRunable implements Runnable { public void run ( ) { while ( true ) { System . out. println ( "hello thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } }
}
public class Demo2 { public static void main ( String [ ] args) throws InterruptedException { Runnable myRunable = new MyRunable ( ) ; Thread thread = new Thread ( myRunable) ; thread. start ( ) ; while ( true ) { System . out. println ( "hello main!" ) ; Thread . sleep ( 1000 ) ; } }
}
繼承Thread,重寫run,但是使用匿名內部類
使用匿名內部類的方式創建出線程
package Thread ; public class Demo3 { public static void main ( String [ ] args) { Thread thread = new Thread ( ) { public void run ( ) { while ( true ) { System . out. println ( "hello Thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } } } ; thread. start ( ) ; while ( true ) { System . out. println ( "hello main!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } }
}
實現Runnable(接口),重寫run,但是使用匿名內部類
package Thread ; public class Demo4 { public static void main ( String [ ] args) { Runnable runnable = new Runnable ( ) { public void run ( ) { System . out. println ( "hello Thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } } ; Thread thread = new Thread ( new Runnable ( ) { public void run ( ) { System . out. println ( "hello Thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } } ) ; thread. start ( ) ; while ( true ) { System . out. println ( "hello main!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } }
}
使用lambda表達式
lambda表達式相當于是匿名內部類的替換寫法
package Thread ; public class Demo5 { public static void main ( String [ ] args) { Thread thread = new Thread ( ( ) -> { while ( true ) { System . out. println ( "hello Thread!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } } ) ; thread. start ( ) ; while ( true ) { System . out. println ( "hello main!" ) ; try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException e) { throw new RuntimeException ( e) ; } } }
}
請說明Thread類中run和start的區別
從方法的區別,及運行結果的區別分別說明 1. start方法可以用來啟動一個新的線程 ,run方法只是一個普通的方法,在主線程中執行 2.start方法只能調用一次 ,run方法可以調用多次 3.調用start方法會執行新的線程,新線程和主線程并發執行,run方法只是線程的入口,start方法調用了系統API,start方法創建出了線程,讓線程再調用run方法 4.run方法和主線程同步執行 ,start方法啟動的線程和主線程異步執行 5.run方法按順序執行 ,start方法調用的線程中的代碼執行順序由線程調度器決定,順序不確定