網站設計中下拉列表怎么做如何刷關鍵詞指數
目錄
🤣一.線程與進程的概念與聯(lián)系:
進程的基本概念:
線程的基本概念:
進程和線程的區(qū)別與聯(lián)系:
🙃代碼執(zhí)行實列:
1.通過繼承Thread父類來實現(xiàn)多線程
2.通過實現(xiàn)Runnable接口來實現(xiàn)多線程:
3.通過Lambda表達式來實現(xiàn)多線程:
😇Thread類的常見屬性和構造方法:
🤣一.線程與進程的概念與聯(lián)系:
一張漫畫,生動闡明進程進程與線程的關系:
進程的基本概念:
- 什么是進程?→
🧐🧐定義:進程是一個具有一定獨立功能的程序在一個數據集合上依次動態(tài)執(zhí)行的過程。進程是一個正在執(zhí)行的程序的實例,包括程序計數器,寄存器和程序變量的當前值。
- 進程有哪些特征?→
1.進程依賴于程序的運行而存在,進程是動態(tài)的,程序是靜態(tài)的
2.進程是操作系統(tǒng)進行資源分配和調度的一個獨立單位(CPU除外,線程是處理器任務調度和執(zhí)行的基本單位);
3.每個進程擁有獨立的地址空間,地址空間包括代碼區(qū),數據區(qū)和堆棧區(qū),進程之間的地址空間是間隔的,互不影響
線程的基本概念:
- 什么是線程?
🦉🦉定義:一個線程就是一個“執(zhí)行流”,每個線程之間都可以按照順序執(zhí)行自己的代碼,多個線程之間“同時”執(zhí)行著多份代碼
- 為什么要用到線程?
?先, "并發(fā)編程" 成為 "剛需":
為了充分利用CPU的資源,避免出現(xiàn)“一核工作,多核圍觀”的情況,我們可以通過編寫特殊的代碼,把多個CPU核心,充分利用起來,這樣的代碼就稱為“并發(fā)編程”,多進程的編程,就是一種典型的并發(fā)編程。雖然多進程能夠解決問題,但是隨著對于效率的要求越來越高,就希望有更好的方法來實現(xiàn)并發(fā)編程。而多進程的編程,最大的問題,就是進程太“重”,創(chuàng)建進程/銷毀進程的開銷比較大(時間,空間),一旦場景需要頻繁的創(chuàng)建和銷毀進程,開銷就非常明顯了→最典型的是服務器開發(fā),針對每一個發(fā)出請求的客戶端,都創(chuàng)建一個單獨的進程,由這個進程負責給客戶端提供服務。為了解決進程開銷比較大的問題,就發(fā)明了線程(Thread),線程可以理解為更加輕量級的進程,也能解決開發(fā)并發(fā)程序的問題,但是創(chuàng)建/銷毀的開銷,要比進程更低。
進程和線程的區(qū)別與聯(lián)系:
- 1.進程包含線程:
一個進程里至少包含一個線程(主線程),也可以包含多個線程。不能沒有線程
2.進程是系統(tǒng)分配資源的基本單位
3.線程是系統(tǒng)調度執(zhí)行的基本單位
- 4.同一個進程里的線程之間,共用一份系統(tǒng)資源(內存,硬盤,網絡寬帶等),尤其是內存資源,就是代碼中定義的變量/對象等信息,編程中,多個線程,是可以共用同一份變量的~
- 5.多線程是當下實現(xiàn)并發(fā)編程的主流方式,通過多線程,就可以充分利用好多核CPU。但是,也不是線程數目越多,就越好,線程數目達到一定程度,把多個核心都利用充分之后,此時繼續(xù)增加線程,無法再提高效率,甚至可能會影響效率(線程的調度,也是有開銷的)
- 6.多個線程之間,可能會互相影響,線程安全問題。一個線程拋出異常,也可能會把其他線程一起帶走?
- 7.多個進程之間,一般不會互相影響,一個進程奔潰了,不會影響到其他進程(進程的隔離性)
🙃代碼執(zhí)行實列:
1.通過繼承Thread父類來實現(xiàn)多線程
//1.通過繼承Thread父類來實現(xiàn)進程
class MyThread extends Thread{@Overridepublic void run(){//這里寫的代碼,就是即將創(chuàng)建的線程,要執(zhí)行的邏輯while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {//throw new RuntimeException(e);e.printStackTrace();}}}
}
public class Demo1 {public static void main(String[] args) throws InterruptedException {MyThread t = new MyThread();//創(chuàng)建線程->運行hello main 和 hello thread 并發(fā)執(zhí)行,同時打印t.start();//run 不會創(chuàng)建線程,也是再主線程中執(zhí)行邏輯//t.run();只循環(huán)打印hello threadwhile(true){System.out.println("hello main");Thread.sleep(1000);}}
}
也可以通過匿名內部類來實現(xiàn):
//通過匿名內部類來創(chuàng)建多線程
public class Demo3 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(){@Overridepublic void run(){while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};t.start();while(true){System.out.println("hello main");Thread.sleep(1000);}}
}
運行結果:
2.通過實現(xiàn)Runnable接口來實現(xiàn)多線程:
//2.通過實現(xiàn)Runnable接口來實現(xiàn)多線程
class MyRunnable implements Runnable{//描述線程里要完成的邏輯是啥@Overridepublic void run() {while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}
public class Demo2 {public static void main(String[] args) throws InterruptedException {MyRunnable runnable = new MyRunnable();Thread t = new Thread(runnable);t.start();while(true){System.out.println("hello main");Thread.sleep(1000);}}
}
也可以通過匿名內部類來實現(xiàn):
public class Demo4 {public static void main(String[] args) throws InterruptedException {//通過匿名內部類來編寫Thread t = new Thread(new Runnable() {@Overridepublic void run() {while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}});t.start();while(true){System.out.println("hello main");Thread.sleep(1000);}}
}
3.通過Lambda表達式來實現(xiàn)多線程:
//通過lambda表達式來進行編寫
public class Demo5 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();while(true){System.out.println("hello main");Thread.sleep(1000);}}
}
😇Thread類的常見屬性和構造方法:
- Thread類常見的構造方法:
- Thread類常見的屬性:
public class Demo6 {public static void main(String[] args) {Thread t = new Thread(()->{while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}},"自定義線程");//->線程名字為自定義線程t.start();System.out.println("線程ID : " + t.getId());System.out.println("線程名字: " + t.getName());System.out.println("線程狀態(tài): " + t.getState());System.out.println("線程執(zhí)行順序:" + t.getPriority());}
}
運行結果:
Runnable正在運行,實際上Java沒有Running這個線程狀態(tài),把正在CPU上運行,和隨時可以調度到CPU上運行的,都統(tǒng)稱為Runnable
- 前臺線程&后臺線程
后臺線程:如果這個線程執(zhí)行過程中,不能阻止進程結束(雖然線程執(zhí)行著,但是進程就要結束了,此時這個線程也會隨之被帶走)這樣的線程就稱為“后臺線程”
前臺線程:如果某個線程執(zhí)行過程中,能阻止進程的結束,此時這個線程就是“前臺線程”
前臺線程和后臺線程,主要是影響程序的退出
public class Demo7 {//設置前public static void main1(String[] args) {Thread t1 = new Thread(()->{while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t1.start();}//設置后public static void main(String[] args) {Thread t2 = new Thread(()->{while(true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});//把 t 設置線程為后臺線程(守護線程),不能在阻止程序結束了t2.setDaemon(true);t2.start();}
}
?結語:?寫博客不僅僅是為了分享學習經歷,同時這也有利于我鞏固知識點,總結該知識點,由于作者水平有限,對文章有任何問題的還請指出,接受大家的批評,讓我改進。同時也希望讀者們不吝嗇你們的點贊+收藏+關注,你們的鼓勵是我創(chuàng)作的最大動力!