激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java多線程之線程,進(jìn)程和Synchronized概念初解

java多線程之線程,進(jìn)程和Synchronized概念初解

2021-02-22 11:43徐劉根 Java教程

這篇文章主要介紹了java多線程之線程,進(jìn)程和Synchronized概念初解,涉及進(jìn)程與線程的簡單概念,實(shí)現(xiàn)多線程的方式,線程安全問題,synchronized修飾符等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。

一、進(jìn)程線程的概念

(1)在傳統(tǒng)的操作系統(tǒng)中,程序并不能獨(dú)立運(yùn)行,作為資源分配和獨(dú)立運(yùn)行的基本單位都是進(jìn)程。

在未配置 os 的系統(tǒng)中,程序的執(zhí)行方式是順序執(zhí)行,即必須在一個程序執(zhí)行完后,才允許另一個程序執(zhí)行;在多道程序環(huán)境下,則允許多個程序并發(fā)執(zhí)行。程序的這兩種執(zhí)行方式間有著顯著的不同。也正是程序并發(fā)執(zhí)行時的這種特征,才導(dǎo)致了在操作系統(tǒng)中引入進(jìn)程的概念。

自從在 20 世紀(jì) 60 年代人們提出了進(jìn)程的概念后,在 os 中一直都是以進(jìn)程作為能擁有資源和獨(dú)立運(yùn)行的基本單位的。直到 20 世紀(jì) 80 年代中期,人們又提出了比進(jìn)程更小的能獨(dú)立運(yùn)行的基本單位——線程(threads),試圖用它來提高系統(tǒng)內(nèi)程序并發(fā)執(zhí)行的程度,從而可進(jìn)一步提高系統(tǒng)的吞吐量。特別是在進(jìn)入 20 世紀(jì) 90 年代后,多處理機(jī)系統(tǒng)得到迅速發(fā)展,線程能比進(jìn)程更好地提高程序的并行執(zhí)行程度,充分地發(fā)揮多處理機(jī)的優(yōu)越性,因而在近幾年所推出的多處理機(jī) os 中也都引入了線程,以改善 os 的性能。

—–以上摘自《計算機(jī)操作系統(tǒng)-湯小丹等編著-3 版》

(2)下圖是來自知乎用戶的解釋:

java多線程之線程,進(jìn)程和Synchronized概念初解

通過上述的大致了解,基本知道線程和進(jìn)程是干什么的了,那么我們下邊給進(jìn)程和線程總結(jié)一下概念:

(3)進(jìn)程(process)是計算機(jī)中的程序關(guān)于某數(shù)據(jù)集合上的一次運(yùn)行活動,是系統(tǒng)進(jìn)行資源分配和調(diào)度的基本單位,是操作系統(tǒng)結(jié)構(gòu)的基礎(chǔ)。在早期面向進(jìn)程設(shè)計的計算機(jī)結(jié)構(gòu)中,進(jìn)程是程序的基本執(zhí)行實(shí)體;在當(dāng)代面向線程設(shè)計的計算機(jī)結(jié)構(gòu)中,進(jìn)程是線程的容器。程序是指令、數(shù)據(jù)及其組織形式的描述,進(jìn)程是程序的實(shí)體。

(4)線程,有時被稱為輕量級進(jìn)程(lightweight process,lwp),是程序執(zhí)行流的最小單元。線程是程序中一個單一的順序控制流程。進(jìn)程內(nèi)一個相對獨(dú)立的、可調(diào)度的執(zhí)行單元,是系統(tǒng)獨(dú)立調(diào)度和分派cpu的基本單位指運(yùn)行中的程序的調(diào)度單位。在單個程序中同時運(yùn)行多個線程完成不同的工作,稱為多線程

(5)進(jìn)程和線程的關(guān)系:

java多線程之線程,進(jìn)程和Synchronized概念初解

二、java實(shí)現(xiàn)多線程方式

(1)繼承thread,重寫run()方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class mythread extends thread {
 
  @override
  public void run() {
    while (true) {
      system.out.println(this.currentthread().getname());
    }
  }
 
  public static void main(string[] args) {
    mythread thread = new mythread();
    thread.start(); //線程啟動的正確方式
  }
}

輸出結(jié)果:

?
1
2
3
4
thread-0
thread-0
thread-0
...

另外,要明白啟動線程的是start()方法而不是run()方法,如果用run()方法,那么他就是一個普通的方法執(zhí)行了。

(2)實(shí)現(xiàn)runable接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class myrunnable implements runnable {
 
  @override
  public void run() {
    system.out.println("123");
  }
 
  public static void main(string[] args) {
    myrunnable myrunnable = new myrunnable();
    thread thread = new thread(myrunnable, "t1");
    thread.start();
  }
}

三、線程安全

線程安全概念:當(dāng)多個線程訪問某一個類(對象或方法)時,這個類始終能表現(xiàn)出正確的行為,那么這個類(對象或方法)就是線程安全的。

線程安全就是多線程訪問時,采用了加鎖機(jī)制,當(dāng)一個線程訪問該類的某個數(shù)據(jù)時,進(jìn)行保護(hù),其他線程不能進(jìn)行訪問直到該線程讀取完,其他線程才可使用。不會出現(xiàn)數(shù)據(jù)不一致或者數(shù)據(jù)污染。 線程不安全就是不提供數(shù)據(jù)訪問保護(hù),有可能出現(xiàn)多個線程先后更改數(shù)據(jù)造成所得到的數(shù)據(jù)是臟數(shù)據(jù)。這里的加鎖機(jī)制常見的如:synchronized

四、synchronized修飾符

(1)synchronized:可以在任意對象及方法上加鎖,而加鎖的這段代碼稱為“互斥區(qū)”或“臨界區(qū)”。

(2)**不使用**synchronized實(shí)例(代碼a):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class mythread extends thread {
    private int count = 5;
    @override
      public void run() {
        count--;
        system.out.println(this.currentthread().getname() + " count:" + count);
    }
    public static void main(string[] args) {
        mythread mythread = new mythread();
        thread thread1 = new thread(mythread, "thread1");
        thread thread2 = new thread(mythread, "thread2");
        thread thread3 = new thread(mythread, "thread3");
        thread thread4 = new thread(mythread, "thread4");
        thread thread5 = new thread(mythread, "thread5");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

輸出的一種結(jié)果如下:

?
1
2
3
4
5
thread3 count:2
thread4 count:1
thread1 count:2
thread2 count:3
thread5 count:0

可以看到,上述的結(jié)果是不正確的,這是因?yàn)椋鄠€線程同時操作run()方法,對count進(jìn)行修改,進(jìn)而造成錯誤。

(3)**使用**synchronized實(shí)例(代碼b):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class mythread extends thread {
 
  private int count = 5;
 
  @override
  public synchronized void run() {
    count--;
    system.out.println(this.currentthread().getname() + " count:" + count);
  }
 
  public static void main(string[] args) {
    mythread mythread = new mythread();
    thread thread1 = new thread(mythread, "thread1");
    thread thread2 = new thread(mythread, "thread2");
    thread thread3 = new thread(mythread, "thread3");
    thread thread4 = new thread(mythread, "thread4");
    thread thread5 = new thread(mythread, "thread5");
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    thread5.start();
  }
}

輸出結(jié)果:

?
1
2
3
4
5
thread1 count:4
thread2 count:3
thread3 count:2
thread5 count:1
thread4 count:0

可以看出代碼a和代碼b的區(qū)別就是在run()方法上加上了synchronized修飾。

說明如下:

當(dāng)多個線程訪問mythread 的run方法的時候,如果使用了synchronized修飾,那個多線程就會以排隊(duì)的方式進(jìn)行處理(這里排隊(duì)是按照cpu分配的先后順序而定的),一個線程想要執(zhí)行synchronized修飾的方法里的代碼,首先是嘗試獲得鎖,如果拿到鎖,執(zhí)行synchronized代碼體的內(nèi)容,如果拿不到鎖的話,這個線程就會不斷的嘗試獲得這把鎖,直到拿到為止,而且多個線程同時去競爭這把鎖,也就是會出現(xiàn)鎖競爭的問題。

五、一個對象有一把鎖!多個線程多個鎖!

何為,一個對象一把鎖,多個線程多個鎖!首先看一下下邊的實(shí)例代碼(代碼c):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class multithread {
 
  private int num = 200;
 
  public synchronized void printnum(string threadname, string tag) {
    if (tag.equals("a")) {
      num = num - 100;
      system.out.println(threadname + " tag a,set num over!");
    } else {
      num = num - 200;
      system.out.println(threadname + " tag b,set num over!");
    }
    system.out.println(threadname + " tag " + tag + ", num = " + num);
  }
 
  public static void main(string[] args) throws interruptedexception {
    final multithread multithread1 = new multithread();
    final multithread multithread2 = new multithread();
 
    new thread(new runnable() {
      public void run() {
        multithread1.printnum("thread1", "a");
      }
    }).start();
 
    thread.sleep(5000);
    system.out.println("等待5秒,確保thread1已經(jīng)執(zhí)行完畢!");
 
    new thread(new runnable() {
      public void run() {
        multithread2.printnum("thread2", "b");
      }
    }).start();
  }
}

輸出結(jié)果:

?
1
2
3
4
5
thread1 tag a,set num over!
thread1 tag a, num = 100
等待5秒,確保thread1已經(jīng)執(zhí)行完畢!
thread2 tag b,set num over!
thread2 tag b, num = 0

可以看出,有兩個對象:multithread1和multithread2,如果多個對象使用同一把鎖的話,那么上述執(zhí)行的結(jié)果就應(yīng)該是:thread2 tag b, num = -100,因此,是每一個對象擁有該對象的鎖的。

關(guān)鍵字synchronized取得的鎖都是對象鎖,而不是把一段代碼或方法當(dāng)做鎖,所以上述實(shí)例代碼c中哪個線程先執(zhí)行synchronized 關(guān)鍵字的方法,那個線程就持有該方法所屬對象的鎖,兩個對象,線程獲得的就是兩個不同對象的不同的鎖,他們互補(bǔ)影響的。

那么,我們在正常的場景的時候,肯定是有一種情況的就是,所有的對象會對一個變量count進(jìn)行操作,那么如何實(shí)現(xiàn)哪?很簡單就是加static,我們知道,用static修改的方法或者變量,在該類的所有對象是具有相同的引用的,這樣的話,無論實(shí)例化多少對象,調(diào)用的都是一個方法,代碼如下(代碼d):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class multithread {
 
  private static int num = 200;
 
  public static synchronized void printnum(string threadname, string tag) {
    if (tag.equals("a")) {
      num = num - 100;
      system.out.println(threadname + " tag a,set num over!");
    } else {
      num = num - 200;
      system.out.println(threadname + " tag b,set num over!");
    }
    system.out.println(threadname + " tag " + tag + ", num = " + num);
  }
 
  public static void main(string[] args) throws interruptedexception {
    final multithread multithread1 = new multithread();
    final multithread multithread2 = new multithread();
 
    new thread(new runnable() {
      public void run() {
        multithread1.printnum("thread1", "a");
      }
    }).start();
 
    thread.sleep(5000);
    system.out.println("等待5秒,確保thread1已經(jīng)執(zhí)行完畢!");
 
    new thread(new runnable() {
      public void run() {
        multithread2.printnum("thread2", "b");
      }
    }).start();
  }
}

輸出結(jié)果:

?
1
2
3
4
5
thread1 tag a,set num over!
thread1 tag a, num = 100
等待5秒,確保thread1已經(jīng)執(zhí)行完畢!
thread2 tag b,set num over!
thread2 tag b, num = -100

可以看出,對變量和方法都加上了static修飾,就可以實(shí)現(xiàn)我們所需要的場景,同時也說明了,對于非靜態(tài)static修飾的方法或變量,是一個對象一把鎖的。

六、對象鎖的同步和異步

(1)同步:synchronized

同步的概念就是共享,我們要知道“共享”這兩個字,如果不是共享的資源,就沒有必要進(jìn)行同步,也就是沒有必要進(jìn)行加鎖;

同步的目的就是為了線程的安全,其實(shí)對于線程的安全,需要滿足兩個最基本的特性:原子性和可見性;

(2)異步:asynchronized

異步的概念就是獨(dú)立,相互之間不受到任何制約,兩者之間沒有任何關(guān)系。

(3)示例代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class myobject {
 
  public void method() {
    system.out.println(thread.currentthread().getname());
  }
 
  public static void main(string[] args) {
    final myobject myobject = new myobject();
 
    thread t1 = new thread(new runnable() {
      public void run() {
        myobject.method();
      }
    }, "t1");
 
    thread t2 = new thread(new runnable() {
      public void run() {
        myobject.method();
      }
    }, "t2");
 
    t1.start();
    t2.start();
  }
}

上述代碼中method()就是異步的方法。

總結(jié)

以上就是本文關(guān)于java多線程之線程,進(jìn)程和synchronized概念初解的全部內(nèi)容,希望對大家有所幫助。如有不足之處,歡迎留言指出,小編會及時回復(fù)大家的。

原文鏈接:http://blog.csdn.net/xlgen157387/article/details/77920497

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 今井夏帆av一区二区 | 国产精品久久在线观看 | 免费一级特黄毛片视频 | 午夜精品久久久久久久96蜜桃 | 在线观看免费视频麻豆 | 亚洲成人网一区 | 精品久久久一 | 激情福利视频 | 91高清观看 | 91精品国产91久久久久久不卞 | 久久久久久久.comav | 国产亚洲精品久久久久婷婷瑜伽 | 国产精品三级a三级三级午夜 | 日本羞羞影院 | 一级成人免费 | 久久免费视频一区 | 国产品久久 | 九色p| 国产精品一区2区3区 | 线观看免费完整aaa 欧美在线一级 | 青青草成人免费视频在线 | 国产精品视频一区二区噜噜 | 1314成人网 | 蜜桃视频在线免费播放 | 成人免费观看49www在线观看 | 被玩坏了的女老师(高h np) | 欧美h版在线观看 | 亚洲影视在线观看 | 久久精品视频首页 | 一区二区三区日韩在线观看 | 国产精品99久久久久久久vr | 草久在线 | 黄色成人短视频 | 国产精品久久久久影院老司 | 少妇一级淫片免费看 | 午夜视频在线 | 免费一级特黄欧美大片勹久久网 | 精品久久久久久久久久久久久 | 亚洲电影免费观看高清完整版在线观 | 少妇的肉体的满足毛片 | 国产中文一区 |