Javaee多線程之進程和線程之間的區(qū)別和聯(lián)系(最新整理)
進程和線程
進程
- 進程:是正在執(zhí)行的程序,是資源分配的基本單位,具有獨立的地址空間
- 操作系統(tǒng)會為其分配CPU和內存
線程
- 線程:引入線程是為了解決進程開銷大,浪費資源的情景,并且多進程并發(fā)效率比較低
- 線程是調度執(zhí)行的基本單位
- 線程之間會相互影響,一個線程掛了,會影響到整個進程都異常結束,線程也自然會結束
進程和線程的區(qū)別
- 進程包含線程,一個進程里面有多個線程或者是一個線程
- 進程和線程都是用來實現(xiàn)并發(fā)編程場景的,但是線程比進程更輕量和高效
- 同一個進程的線程之間共用同一份資源(內存和硬盤),省去了申請資源的開銷
- 進程和進程之間都是獨立存在的,不會相互影響,同一個進程中,線程和線程之間會相互影響(線程安全問題 + 線程出現(xiàn)異常)
- 進程是分配資源的基本單位,線程是調度執(zhí)行的基本單位
創(chuàng)建線程的五種寫法
繼承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);
}
}
}
}
// 創(chuàng)建線程
public class Demo1 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new MyThread();
// 使用start方法可以間接調用run方法
// start和 run都時Thread的成員
// run 只是線程的入口(描述了線程要做什么事情)
// start才是真正調用了系統(tǒng)的API,在系統(tǒng)中創(chuàng)建出了線程,讓線程調用 run
thread.start();
// 從這句開始程序就并發(fā)執(zhí)行,一邊執(zhí)行hello main,一邊執(zhí)行hello Thread
// 兵分兩路進行執(zhí)行
// 并發(fā) == 并行 + 并發(fā)
while(true){
System.out.println("hello main!");
Thread.sleep(1000);
}
// 先執(zhí)行main,再執(zhí)行的是Thread,先執(zhí)行主線程
}
}實現(xiàn)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 {
// 這個接口就是用來實現(xiàn)多態(tài)的
Runnable myRunable = new MyRunable();
Thread thread = new Thread(myRunable);
thread.start();
while(true){
System.out.println("hello main!");
Thread.sleep(1000);
}
}
}繼承Thread,重寫run,但是使用匿名內部類
- 使用匿名內部類的方式創(chuàng)建出線程
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);
}
}
}
}實現(xiàn)Runnable(接口),重寫run,但是使用匿名內部類
package Thread;
public class Demo4 {
public static void main(String[] args) {
// 法一:創(chuàng)建實例
Runnable runnable = new Runnable(){
public void run(){
System.out.println("hello Thread!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
// 法二:創(chuàng)建匿名對象
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 thread = new Thread(runnable);
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的區(qū)別
從方法的區(qū)別,及運行結果的區(qū)別分別說明
1. start方法可以用來啟動一個新的線程,run方法只是一個普通的方法,在主線程中執(zhí)行
2.start方法只能調用一次,run方法可以調用多次
3.調用start方法會執(zhí)行新的線程,新線程和主線程并發(fā)執(zhí)行,run方法只是線程的入口,start方法調用了系統(tǒng)API,start方法創(chuàng)建出了線程,讓線程再調用run方法
4.run方法和主線程同步執(zhí)行,start方法啟動的線程和主線程異步執(zhí)行
5.run方法按順序執(zhí)行,start方法調用的線程中的代碼執(zhí)行順序由線程調度器決定,順序不確定
到此這篇關于Javaee多線程之進程和線程之間的區(qū)別和聯(lián)系(最新整理)的文章就介紹到這了,更多相關java進程和線程區(qū)別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java使用Hashtable過濾數(shù)組中重復值的方法
這篇文章主要介紹了java使用Hashtable過濾數(shù)組中重復值的方法,涉及java數(shù)組遍歷及過濾的相關技巧,需要的朋友可以參考下2016-08-08
手把手教你設置IntelliJ IDEA 的彩色代碼主題的圖文教程
本文給出一系列 IntelliJ IDEA 代碼的彩色主題,感興趣的朋友一起看看吧2018-01-01
支付寶二面:使用?try-catch?捕獲異常會影響性能嗎(推薦)
這篇文章主要介紹了支付寶二面:使用?try-catch?捕獲異常會影響性能嗎,需要注意的是,JVM?中?異常處理的catch語句不再由字節(jié)碼指令來實現(xiàn)(很早之前通過?jsr和?ret指令來完成,需要的朋友可以參考下2023-03-03

