一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - java多線程之線程,進程和Synchronized概念初解

java多線程之線程,進程和Synchronized概念初解

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

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

一、進程線程的概念

(1)在傳統的操作系統中,程序并不能獨立運行,作為資源分配和獨立運行的基本單位都是進程。

在未配置 os 的系統中,程序的執行方式是順序執行,即必須在一個程序執行完后,才允許另一個程序執行;在多道程序環境下,則允許多個程序并發執行。程序的這兩種執行方式間有著顯著的不同。也正是程序并發執行時的這種特征,才導致了在操作系統中引入進程的概念。

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

—–以上摘自《計算機操作系統-湯小丹等編著-3 版》

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

java多線程之線程,進程和Synchronized概念初解

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

(3)進程(process)是計算機中的程序關于某數據集合上的一次運行活動,是系統進行資源分配和調度的基本單位,是操作系統結構的基礎。在早期面向進程設計的計算機結構中,進程是程序的基本執行實體;在當代面向線程設計的計算機結構中,進程是線程的容器。程序是指令、數據及其組織形式的描述,進程是程序的實體。

(4)線程,有時被稱為輕量級進程(lightweight process,lwp),是程序執行流的最小單元。線程是程序中一個單一的順序控制流程。進程內一個相對獨立的、可調度的執行單元,是系統獨立調度和分派cpu的基本單位指運行中的程序的調度單位。在單個程序中同時運行多個線程完成不同的工作,稱為多線程

(5)進程和線程的關系:

java多線程之線程,進程和Synchronized概念初解

二、java實現多線程方式

(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(); //線程啟動的正確方式
  }
}

輸出結果:

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

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

(2)實現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();
  }
}

三、線程安全

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

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

四、synchronized修飾符

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

(2)**不使用**synchronized實例(代碼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();
    }
}

輸出的一種結果如下:

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

可以看到,上述的結果是不正確的,這是因為,多個線程同時操作run()方法,對count進行修改,進而造成錯誤。

(3)**使用**synchronized實例(代碼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();
  }
}

輸出結果:

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

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

說明如下:

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

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

何為,一個對象一把鎖,多個線程多個鎖!首先看一下下邊的實例代碼(代碼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已經執行完畢!");
 
    new thread(new runnable() {
      public void run() {
        multithread2.printnum("thread2", "b");
      }
    }).start();
  }
}

輸出結果:

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

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

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

那么,我們在正常的場景的時候,肯定是有一種情況的就是,所有的對象會對一個變量count進行操作,那么如何實現哪?很簡單就是加static,我們知道,用static修改的方法或者變量,在該類的所有對象是具有相同的引用的,這樣的話,無論實例化多少對象,調用的都是一個方法,代碼如下(代碼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已經執行完畢!");
 
    new thread(new runnable() {
      public void run() {
        multithread2.printnum("thread2", "b");
      }
    }).start();
  }
}

輸出結果:

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

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

六、對象鎖的同步和異步

(1)同步:synchronized

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

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

(2)異步:asynchronized

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

(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()就是異步的方法。

總結

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年人在线免费看 | 99国产精品免费观看视频 | 石原莉奈被店长侵犯免费 | 全彩孕交漫画福利啪啪吧 | bban女同系列022在线观看 | 欧美日韩亚洲高清不卡一区二区三区 | 日韩在线二区 | 胸大的姑娘中文字幕视频 | 丰满岳乱妇在线观看视频国产 | 999国产| 猛h辣h高h文湿校园1v1 | 无套内谢大学生A片 | 日本精品中文字幕在线播放 | 无敌在线视频观看免费 | 奇米888在线看奇米999 | 99久久国产综合精麻豆 | 超时空要爱国语完整版在线 | 高清一区高清二区视频 | 国内交换一区二区三区 | 日本高清全集免费观看 | 亚洲精美视频 | 扒开腚眼子视频大全 | 俄罗斯一级大片 | 亚洲国产综合自在线另类 | 免费人成在线观看69式小视频 | 91免费永久在线地址 | 污翼鸟| 国产视频二| 国产九九在线观看播放 | 动漫美丽妇人1~2在线看 | jj免费视频 | 91啪在线观看国产在线 | 亚洲一区二区三区久久精品 | 我要色色网| 亚洲欧美另类综合 | 范冰冰上面好大下面好紧 | 狠狠色婷婷狠狠狠亚洲综合 | 亚洲国产欧美在线人成 | 丝袜爆操 | 久久成人a毛片免费观看网站 | 精品第一国产综合精品蜜芽 |