国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務器之家 - 編程語言 - JAVA教程 - java多線程編程之Synchronized塊同步方法

java多線程編程之Synchronized塊同步方法

2020-03-06 20:04lijiao JAVA教程

這篇文章主要介紹了java多線程編程之Synchronized塊同步方法,synchronized關鍵字又稱同步鎖,當方法執行完后,會自動釋放鎖鎖,只有一個線程能進入此方法,看看下文中各種例子對synchronized的詳細解釋

文章分享了4個例子對synchronized的詳細解釋

1、是否加synchronized關鍵字的不同

?
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
36
37
38
39
public class ThreadTest {
 
  public static void main(String[] args) {
    Example example = new Example();
 
    Thread t1 = new Thread1(example);
    Thread t2 = new Thread1(example);
    t1.start();
    t2.start();
  }
}
 
class Example {
  public synchronized void excute() {
    for (int i = 0; i < 5; ++i) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("excute:" + i);
    }
 
  }
 
}
 
class Thread1 extends Thread {
  private Example example;
 
  public Thread1(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute();
  }
}

加了synchronized關鍵字的輸出結果如下

會先輸出一組0-4,接著再輸出下一組,兩個線程順序執行

excute:0
excute:1
excute:2
excute:3
excute:4
excute:0
excute:1
excute:2
excute:3
excute:4

沒加synchronized關鍵字的輸出結果如下

兩個線程同時執行excute方法,同時并發的

excute:0
excute:0
excute:1
excute:1
excute:2
excute:2
excute:3
excute:3
excute:4
excute:4

2、多個方法的多線程情況

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class ThreadTest {
 
  public static void main(String[] args) {
    Example example = new Example();
 
    Thread t1 = new Thread1(example);
    Thread t2 = new Thread2(example);
    t1.start();
    t2.start();
  }
}
 
class Example {
  public synchronized void excute() {
    for (int i = 0; i < 5; ++i) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("excute:" + i);
    }
  }
  public synchronized void excute1() {
    for (int i = 0; i < 5; ++i) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("excute1:" + i);
    }
  }
 
}
 
class Thread1 extends Thread {
  private Example example;
 
  public Thread1(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute();
  }
}
 
class Thread2 extends Thread {
  private Example example;
 
  public Thread2(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute1();
  }
}

執行結果如下

同樣是順序執行,執行完一個線程再執行另一個線程

excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4

如果去掉synchronized關鍵字,則兩個方法并發執行,并沒有相互影響。

但是如例子程序中所寫,即便是兩個方法:

執行結果永遠是執行完一個線程的輸出再執行另一個線程的。  

說明:

  如果一個對象有多個synchronized方法,某一時刻某個線程已經進入到了某個synchronized方法,那么在該方法沒有執行完畢前,其他線程是無法訪問該對象的任何synchronized方法的。

結論:

  當synchronized關鍵字修飾一個方法的時候,該方法叫做同步方法。

  Java中的每個對象都有一個鎖(lock),或者叫做監視器(monitor),當一個線程訪問某個對象的synchronized方法時,將該對象上鎖,其他任何線程都無法再去訪問該對象的synchronized方法了(這里是指所有的同步方法,而不僅僅是同一個方法),直到之前的那個線程執行方法完畢后(或者是拋出了異常),才將該對象的鎖釋放掉,其他線程才有可能再去訪問該對象的synchronized方法。

  注意這時候是給對象上鎖,如果是不同的對象,則各個對象之間沒有限制關系。

  嘗試在代碼中構造第二個線程對象時傳入一個新的Example對象,則兩個線程的執行之間沒有什么制約關系。

3、靜態的同步方法

當一個synchronized關鍵字修飾的方法同時又被static修飾,之前說過,非靜態的同步方法會將對象上鎖,但是靜態方法不屬于對象,而是屬于類,它會將這個方法所在的類的Class對象上鎖。

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class ThreadTest {
 
  public static void main(String[] args) {
    Example example = new Example();
    Example example2 = new Example();
    Thread t1 = new Thread1(example);
    Thread t2 = new Thread2(example2);
    t1.start();
    t2.start();
  }
}
 
class Example {
  public synchronized static void excute() {
    for (int i = 0; i < 5; ++i) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("excute:" + i);
    }
  }
  public synchronized static void excute1() {
    for (int i = 0; i < 5; ++i) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("excute1:" + i);
    }
  }
 
}
 
class Thread1 extends Thread {
  private Example example;
 
  public Thread1(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute();
  }
}
 
class Thread2 extends Thread {
  private Example example;
 
  public Thread2(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute1();
  }
}

執行結果如下

excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4

如果沒有static修飾符,兩個線程分別傳入不同的對象,則會同時并發執行

所以如果是靜態方法的情況(execute()和execute2()都加上static關鍵字),即便是向兩個線程傳入不同的Example對象,這兩個線程仍然是互相制約的,必須先執行完一個,再執行下一個。

結論:

  如果某個synchronized方法是static的,那么當線程訪問該方法時,它鎖的并不是synchronized方法所在的對象,而是synchronized方法所在的類所對應的Class對象。Java中,無論一個類有多少個對象,這些對象會對應唯一一個Class對象,因此當線程分別訪問同一個類的兩個對象的兩個static,synchronized方法時,它們的執行順序也是順序的,也就是說一個線程先去執行方法,執行完畢后另一個線程才開始。

4.synchronized塊

synchronized(object)

{     

}

表示線程在執行的時候會將object對象上鎖。(注意這個對象可以是任意類的對象,也可以使用this關鍵字)。

這樣就可以自行規定上鎖對象。

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class ThreadTest {
 
  public static void main(String[] args) {
    Example example = new Example();
    Thread t1 = new Thread1(example);
    Thread t2 = new Thread2(example);
    t1.start();
    t2.start();
  }
}
 
class Example {
  public void excute() {
    synchronized (this) {
      for (int i = 0; i < 5; ++i) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("excute:" + i);
      }
    }
    
  }
  public void excute1() {
    synchronized (this) {
      for (int i = 0; i < 5; ++i) {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        System.out.println("excute1:" + i);
      }
    }
    
  }
 
}
 
class Thread1 extends Thread {
  private Example example;
 
  public Thread1(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute();
  }
}
 
class Thread2 extends Thread {
  private Example example;
 
  public Thread2(Example example) {
    this.example = example;
  }
 
  @Override
  public void run() {
    example.excute1();
  }
}

執行結果如下

excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4

例子程序4所達到的效果和例子程序2的效果一樣,都是使得兩個線程的執行順序進行,而不是并發進行,當一個線程執行時,將object對象鎖住,另一個線程就不能執行對應的塊。

synchronized方法實際上等同于用一個synchronized塊包住方法中的所有語句,然后在synchronized塊的括號中傳入this關鍵字。當然,如果是靜態方法,需要鎖定的則是class對象。  

可能一個方法中只有幾行代碼會涉及到線程同步問題,所以synchronized塊比synchronized方法更加細粒度地控制了多個線程的訪問,只有synchronized塊中的內容不能同時被多個線程所訪問,方法中的其他語句仍然可以同時被多個線程所訪問(包括synchronized塊之前的和之后的)。

結論:

  synchronized方法是一種粗粒度的并發控制,某一時刻,只能有一個線程執行該synchronized方法;

  synchronized塊則是一種細粒度的并發控制,只會將塊中的代碼同步,位于方法內、synchronized塊之外的其他代碼是可以被多個線程同時訪問到的。

以上就是關于java多線程編程Synchronized塊同步方法,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
455
主站蜘蛛池模板: 国产a区| 亚洲国产成人一区二区精品区 | 一区二区日韩精品 | jizz中国女人高潮 | 久久久久久久久久久蜜桃 | 91xxx在线观看 | 午夜视频免费在线观看 | 日韩av电影在线观看 | av在线免费观看网址 | 国产精品久久久久久久久久99 | 在线成人免费视频 | 亚洲一区国产精品 | 高清一区二区三区 | 婷婷丁香综合 | 亚洲欧洲视频 | 成人精品 | 日韩av电影在线免费观看 | 日韩电影一区二区三区 | 黄网页在线观看 | 一 级 黄 色 片免费网站 | 欧美日韩在线免费观看 | 精品久久久久久久久久久久 | 搞黄免费视频 | 91在线日韩| 欧美日韩在线免费 | 在线观看日韩精品 | 自拍偷拍亚洲 | 91久久综合亚洲鲁鲁五月天 | av免费网站| 久久久久久久国产 | 在线精品一区 | 依人在线免费视频 | 九九九九国产 | 综合二区 | 99精品国产热久久91蜜凸 | 老熟女毛片| 精彩视频一区二区三区 | 亚洲精品久久久久久久久久久 | 午夜精品久久久久久久久 | 91精品国产综合久久精品 | 综合五月网 |