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

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

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

服務(wù)器之家 - 編程語言 - JAVA教程 - Java多線程編程中synchronized線程同步的教程

Java多線程編程中synchronized線程同步的教程

2020-05-24 11:49JinhaoPlus JAVA教程

Java的synchronized關(guān)鍵字可以修飾方法和對象來構(gòu)建線程間的同步,這里我們就來共同學(xué)習(xí)Java多線程編程中synchronized線程同步的教程:

0.關(guān)于線程同步
(1)為什么需要同步多線程?

線程的同步是指讓多個(gè)運(yùn)行的線程在一起良好地協(xié)作,達(dá)到讓多線程按要求合理地占用釋放資源。我們采用Java中的同步代碼塊和同步方法達(dá)到這樣的目的。比如這樣的解決多線程無固定序執(zhí)行的問題:

?
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 TwoThreadTest {
  public static void main(String[] args) {
    Thread th1= new MyThread1();
    Thread th2= new MyThread2();
    th1.start();
    th2.start();
  }
}
 
class MyThread2 extends Thread{
  @Override
  public void run() {
    for( int i=0;i<10;i++)
      System. out.println( "thread 1 counter:"+i);
  }
}
 
class MyThread1 extends Thread{
  @Override
  public void run() {
    for( int i=0;i<10;i++)
      System. out.println( "thread 2 counter:"+i);
  
}

這種狀態(tài)下多線程執(zhí)行的結(jié)果是隨機(jī)地去任意插入執(zhí)行,這完全取決于JVM對于線程的調(diào)度,在很多要求定序執(zhí)行的情況下,這種隨機(jī)執(zhí)行的狀態(tài)顯然是不合要求的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ThreadTest {
  public static void main(String[] args) {
    MyThread thread = new MyThread();
    Thread th1= new Thread(thread);
    Thread th2= new Thread(thread);
    th1.start();
    th2.start();
  }
 
}
 
class MyThread implements Runnable{
  @Override
  public synchronized void run() {
    for( int i=0;i<10;i++)
      System. out.println(Thread. currentThread().getName()+" counter:"+i);
  }
}

使用了同步方法后我們就可以控制線程獨(dú)占執(zhí)行體對象,這樣在執(zhí)行的過程中就可以使得線程將執(zhí)行體上的任務(wù)一次性執(zhí)行完后退出鎖定狀態(tài),JVM再調(diào)度另一個(gè)線程進(jìn)來一次性運(yùn)行執(zhí)行體內(nèi)的任務(wù)。

(2)線程創(chuàng)建運(yùn)行的范式:
在以前我們也有自己的線程創(chuàng)建和運(yùn)行的編程范式,一般是定義一個(gè)執(zhí)行類重寫run()方法,但是這種方式將執(zhí)行體和執(zhí)行的任務(wù)放在了一起,從軟件工程的角度來看不利于解耦。一個(gè)線程的執(zhí)行的意思是說線程通過執(zhí)行對象執(zhí)行了某個(gè)對象的某個(gè)任務(wù),從這個(gè)角度來說,將任務(wù)的規(guī)定者從執(zhí)行類中分離出來可以使得多線程編程的各個(gè)角色明晰出來,進(jìn)而獲得良好地解耦,以下就是線程創(chuàng)建和執(zhí)行的編程范式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class FormalThreadClass {
  public static void main(String[] args) {
    Thread thread = new Thread( new MyRunnable());
    thread.start();
  }
}
 
class MyRunnable implements Runnable{
  MyTask myTask = new MyTask();
  @Override
  public void run() {
    myTask.doTask();
  }
}
 
class MyTask{
  public void doTask() {
    System. out.println( "This is real Tasking");
  }
}


1. synchronized原理
在java中,每一個(gè)對象有且僅有一個(gè)同步鎖。這也意味著,同步鎖是依賴于對象而存在。
當(dāng)我們調(diào)用某對象的synchronized方法時(shí),就獲取了該對象的同步鎖。例如,synchronized(obj)就獲取了“obj這個(gè)對象”的同步鎖。
不同線程對同步鎖的訪問是互斥的。也就是說,某時(shí)間點(diǎn),對象的同步鎖只能被一個(gè)線程獲取到!通過同步鎖,我們就能在多線程中,實(shí)現(xiàn)對“對象/方法”的互斥訪問。 例如,現(xiàn)在有兩個(gè)線程A和線程B,它們都會訪問“對象obj的同步鎖”。假設(shè),在某一時(shí)刻,線程A獲取到“obj的同步鎖”并在執(zhí)行一些操作;而此時(shí),線程B也企圖獲取“obj的同步鎖” —— 線程B會獲取失敗,它必須等待,直到線程A釋放了“該對象的同步鎖”之后線程B才能獲取到“obj的同步鎖”從而才可以運(yùn)行。

2. synchronized基本規(guī)則
我們將synchronized的基本規(guī)則總結(jié)為下面3條,并通過實(shí)例對它們進(jìn)行說明。
第一條: 當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程對“該對象”的該“synchronized方法”或者“synchronized代碼塊”的訪問將被阻塞。
第二條: 當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程仍然可以訪問“該對象”的非同步代碼塊。
第三條: 當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程對“該對象”的其他的“synchronized方法”或者“synchronized代碼塊”的訪問將被阻塞。
(1)第一條:
當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程對“該對象”的該“synchronized方法”或者“synchronized代碼塊”的訪問將被阻塞。 下面是“synchronized代碼塊”對應(yīng)的演示程序。

?
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
class MyRunable implements Runnable {
 
 @Override
 public void run() {
  synchronized(this) {
   try {
    for (int i = 0; i < 5; i++) {
     Thread.sleep(100); // 休眠100ms
     System.out.println(Thread.currentThread().getName() + " loop " + i);
    }
   } catch (InterruptedException ie) {
   }
  }
 }
}
 
public class Demo1_1 {
 
 public static void main(String[] args) {
  Runnable demo = new MyRunable();  // 新建“Runnable對象”
 
  Thread t1 = new Thread(demo, "t1"); // 新建“線程t1”, t1是基于demo這個(gè)Runnable對象
  Thread t2 = new Thread(demo, "t2"); // 新建“線程t2”, t2是基于demo這個(gè)Runnable對象
  t1.start();       // 啟動(dòng)“線程t1”
  t2.start();       // 啟動(dòng)“線程t2”
 }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t1 loop 0
t1 loop 1
t1 loop 2
t1 loop 3
t1 loop 4
t2 loop 0
t2 loop 1
t2 loop 2
t2 loop 3
t2 loop 4

結(jié)果說明:run()方法中存在“synchronized(this)代碼塊”,而且t1和t2都是基于"demo這個(gè)Runnable對象"創(chuàng)建的線程。這就意味著,我們可以將synchronized(this)中的this看作是“demo這個(gè)Runnable對象”;因此,線程t1和t2共享“demo對象的同步鎖”。所以,當(dāng)一個(gè)線程運(yùn)行的時(shí)候,另外一個(gè)線程必須等待“運(yùn)行線程”釋放“demo的同步鎖”之后才能運(yùn)行。
如果你確認(rèn),你搞清楚這個(gè)問題了。那我們將上面的代碼進(jìn)行修改,然后再運(yùn)行看看結(jié)果怎么樣,看看你是否會迷糊。修改后的源碼如下:

?
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
class MyThread extends Thread {
 
 public MyThread(String name) {
  super(name);
 }
 
 @Override
 public void run() {
  synchronized(this) {
   try {
    for (int i = 0; i < 5; i++) {
     Thread.sleep(100); // 休眠100ms
     System.out.println(Thread.currentThread().getName() + " loop " + i);
    }
   } catch (InterruptedException ie) {
   }
  }
 }
}
 
public class Demo1_2 {
 
 public static void main(String[] args) {
  Thread t1 = new MyThread("t1"); // 新建“線程t1”
  Thread t2 = new MyThread("t2"); // 新建“線程t2”
  t1.start();       // 啟動(dòng)“線程t1”
  t2.start();       // 啟動(dòng)“線程t2”
 }
}

代碼說明:比較Demo1_2 和 Demo1_1,我們發(fā)現(xiàn),Demo1_2中的MyThread類是直接繼承于Thread,而且t1和t2都是MyThread子線程。
幸運(yùn)的是,在“Demo1_2的run()方法”也調(diào)用了synchronized(this),正如“Demo1_1的run()方法”也調(diào)用了synchronized(this)一樣!
那么,Demo1_2的執(zhí)行流程是不是和Demo1_1一樣呢?運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t1 loop 0
t2 loop 0
t1 loop 1
t2 loop 1
t1 loop 2
t2 loop 2
t1 loop 3
t2 loop 3
t1 loop 4
t2 loop 4

結(jié)果說明:
如果這個(gè)結(jié)果一點(diǎn)也不令你感到驚訝,那么我相信你對synchronized和this的認(rèn)識已經(jīng)比較深刻了。否則的話,請繼續(xù)閱讀這里的分析。
synchronized(this)中的this是指“當(dāng)前的類對象”,即synchronized(this)所在的類對應(yīng)的當(dāng)前對象。它的作用是獲取“當(dāng)前對象的同步鎖”。
對于Demo1_2中,synchronized(this)中的this代表的是MyThread對象,而t1和t2是兩個(gè)不同的MyThread對象,因此t1和t2在執(zhí)行synchronized(this)時(shí),獲取的是不同對象的同步鎖。對于Demo1_1對而言,synchronized(this)中的this代表的是MyRunable對象;t1和t2共同一個(gè)MyRunable對象,因此,一個(gè)線程獲取了對象的同步鎖,會造成另外一個(gè)線程等待。
(2)第二條:
當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程仍然可以訪問“該對象”的非同步代碼塊。
下面是“synchronized代碼塊”對應(yīng)的演示程序。

?
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
class Count {
 
 // 含有synchronized同步塊的方法
 public void synMethod() {
  synchronized(this) {
   try {
    for (int i = 0; i < 5; i++) {
     Thread.sleep(100); // 休眠100ms
     System.out.println(Thread.currentThread().getName() + " synMethod loop " + i);
    }
   } catch (InterruptedException ie) {
   }
  }
 }
 
 // 非同步的方法
 public void nonSynMethod() {
  try {
   for (int i = 0; i < 5; i++) {
    Thread.sleep(100);
    System.out.println(Thread.currentThread().getName() + " nonSynMethod loop " + i);
   }
  } catch (InterruptedException ie) {
  }
 }
}
 
public class Demo2 {
 
 public static void main(String[] args) {
  final Count count = new Count();
  // 新建t1, t1會調(diào)用“count對象”的synMethod()方法
  Thread t1 = new Thread(
    new Runnable() {
     @Override
     public void run() {
      count.synMethod();
     }
    }, "t1");
 
  // 新建t2, t2會調(diào)用“count對象”的nonSynMethod()方法
  Thread t2 = new Thread(
    new Runnable() {
     @Override
     public void run() {
      count.nonSynMethod();
     }
    }, "t2");
 
 
  t1.start(); // 啟動(dòng)t1
  t2.start(); // 啟動(dòng)t2
 }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t1 synMethod loop 0
t2 nonSynMethod loop 0
t1 synMethod loop 1
t2 nonSynMethod loop 1
t1 synMethod loop 2
t2 nonSynMethod loop 2
t1 synMethod loop 3
t2 nonSynMethod loop 3
t1 synMethod loop 4
t2 nonSynMethod loop 4

結(jié)果說明:
主線程中新建了兩個(gè)子線程t1和t2。t1會調(diào)用count對象的synMethod()方法,該方法內(nèi)含有同步塊;而t2則會調(diào)用count對象的nonSynMethod()方法,該方法不是同步方法。t1運(yùn)行時(shí),雖然調(diào)用synchronized(this)獲取“count的同步鎖”;但是并沒有造成t2的阻塞,因?yàn)閠2沒有用到“count”同步鎖。
(3)第三條:
當(dāng)一個(gè)線程訪問“某對象”的“synchronized方法”或者“synchronized代碼塊”時(shí),其他線程對“該對象”的其他的“synchronized方法”或者“synchronized代碼塊”的訪問將被阻塞。
我們將上面的例子中的nonSynMethod()方法體的也用synchronized(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
class Count {
 
 // 含有synchronized同步塊的方法
 public void synMethod() {
  synchronized(this) {
   try {
    for (int i = 0; i < 5; i++) {
     Thread.sleep(100); // 休眠100ms
     System.out.println(Thread.currentThread().getName() + " synMethod loop " + i);
    }
   } catch (InterruptedException ie) {
   }
  }
 }
 
 // 也包含synchronized同步塊的方法
 public void nonSynMethod() {
  synchronized(this) {
   try {
    for (int i = 0; i < 5; i++) {
     Thread.sleep(100);
     System.out.println(Thread.currentThread().getName() + " nonSynMethod loop " + i);
    }
   } catch (InterruptedException ie) {
   }
  }
 }
}
 
public class Demo3 {
 
 public static void main(String[] args) {
  final Count count = new Count();
  // 新建t1, t1會調(diào)用“count對象”的synMethod()方法
  Thread t1 = new Thread(
    new Runnable() {
     @Override
     public void run() {
      count.synMethod();
     }
    }, "t1");
 
  // 新建t2, t2會調(diào)用“count對象”的nonSynMethod()方法
  Thread t2 = new Thread(
    new Runnable() {
     @Override
     public void run() {
      count.nonSynMethod();
     }
    }, "t2");
 
 
  t1.start(); // 啟動(dòng)t1
  t2.start(); // 啟動(dòng)t2
 }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t1 synMethod loop 0
t1 synMethod loop 1
t1 synMethod loop 2
t1 synMethod loop 3
t1 synMethod loop 4
t2 nonSynMethod loop 0
t2 nonSynMethod loop 1
t2 nonSynMethod loop 2
t2 nonSynMethod loop 3
t2 nonSynMethod loop 4

結(jié)果說明:
主線程中新建了兩個(gè)子線程t1和t2。t1和t2運(yùn)行時(shí)都調(diào)用synchronized(this),這個(gè)this是Count對象(count),而t1和t2共用count。因此,在t1運(yùn)行時(shí),t2會被阻塞,等待t1運(yùn)行釋放“count對象的同步鎖”,t2才能運(yùn)行。

3. synchronized方法 和 synchronized代碼塊
“synchronized方法”是用synchronized修飾方法,而 “synchronized代碼塊”則是用synchronized修飾代碼塊。
synchronized方法示例

?
1
2
3
4
5
6
7
8
9
public synchronized void foo1() {
  System.out.println("synchronized methoed");
}
synchronized代碼塊
public void foo2() {
  synchronized (this) {
    System.out.println("synchronized methoed");
  }
}

synchronized代碼塊中的this是指當(dāng)前對象。也可以將this替換成其他對象,例如將this替換成obj,則foo2()在執(zhí)行synchronized(obj)時(shí)就獲取的是obj的同步鎖。
synchronized代碼塊可以更精確的控制沖突限制訪問區(qū)域,有時(shí)候表現(xiàn)更高效率。下面通過一個(gè)示例來演示:

?
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
// Demo4.java的源碼
public class Demo4 {
 
  public synchronized void synMethod() {
    for(int i=0; i<1000000; i++)
      ;
  }
 
  public void synBlock() {
    synchronized( this ) {
      for(int i=0; i<1000000; i++)
        ;
    }
  }
 
  public static void main(String[] args) {
    Demo4 demo = new Demo4();
 
    long start, diff;
    start = System.currentTimeMillis();        // 獲取當(dāng)前時(shí)間(millis)
    demo.synMethod();                // 調(diào)用“synchronized方法”
    diff = System.currentTimeMillis() - start;    // 獲取“時(shí)間差值”
    System.out.println("synMethod() : "+ diff);
 
    start = System.currentTimeMillis();        // 獲取當(dāng)前時(shí)間(millis)
    demo.synBlock();                // 調(diào)用“synchronized方法塊”
    diff = System.currentTimeMillis() - start;    // 獲取“時(shí)間差值”
    System.out.println("synBlock() : "+ diff);
  }
}

(某一次)執(zhí)行結(jié)果:

?
1
2
synMethod() : 11
synBlock() : 3

4. 實(shí)例鎖 和 全局鎖
實(shí)例鎖 -- 鎖在某一個(gè)實(shí)例對象上。如果該類是單例,那么該鎖也具有全局鎖的概念。
(1)實(shí)例鎖對應(yīng)的就是synchronized關(guān)鍵字。
(2)全局鎖 -- 該鎖針對的是類,無論實(shí)例多少個(gè)對象,那么線程都共享該鎖。
全局鎖對應(yīng)的就是static synchronized(或者是鎖在該類的class或者classloader對象上)。
關(guān)于“實(shí)例鎖”和“全局鎖”有一個(gè)很形象的例子:

?
1
2
3
4
5
6
pulbic class Something {
  public synchronized void isSyncA(){}
  public synchronized void isSyncB(){}
  public static synchronized void cSyncA(){}
  public static synchronized void cSyncB(){}
}

假設(shè),Something有兩個(gè)實(shí)例x和y。分析下面4組表達(dá)式獲取的鎖的情況。
(1) x.isSyncA()與x.isSyncB()
(2) x.isSyncA()與y.isSyncA()
(3) x.cSyncA()與y.cSyncB()
(4) x.isSyncA()與Something.cSyncA()

(1) 不能被同時(shí)訪問。
因?yàn)閕sSyncA()和isSyncB()都是訪問同一個(gè)對象(對象x)的同步鎖!

?
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
// LockTest1.java的源碼
class Something {
  public synchronized void isSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public synchronized void isSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
}
 
public class LockTest1 {
 
  Something x = new Something();
  Something y = new Something();
 
  // 比較(01) x.isSyncA()與x.isSyncB()
  private void test1() {
    // 新建t11, t11會調(diào)用 x.isSyncA()
    Thread t11 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            x.isSyncA();
          }
        }, "t11");
 
    // 新建t12, t12會調(diào)用 x.isSyncB()
    Thread t12 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            x.isSyncB();
          }
        }, "t12");
 
 
    t11.start(); // 啟動(dòng)t11
    t12.start(); // 啟動(dòng)t12
  }
 
  public static void main(String[] args) {
    LockTest1 demo = new LockTest1();
    demo.test1();
  }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t11 : isSyncA
t11 : isSyncA
t11 : isSyncA
t11 : isSyncA
t11 : isSyncA
t12 : isSyncB
t12 : isSyncB
t12 : isSyncB
t12 : isSyncB
t12 : isSyncB

(2) 可以同時(shí)被訪問

因?yàn)樵L問的不是同一個(gè)對象的同步鎖,x.isSyncA()訪問的是x的同步鎖,而y.isSyncA()訪問的是y的同步鎖。

?
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
67
68
69
70
71
72
73
74
75
76
// LockTest2.java的源碼
class Something {
  public synchronized void isSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public synchronized void isSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
}
 
public class LockTest2 {
 
  Something x = new Something();
  Something y = new Something();
 
  // 比較(02) x.isSyncA()與y.isSyncA()
  private void test2() {
    // 新建t21, t21會調(diào)用 x.isSyncA()
    Thread t21 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            x.isSyncA();
          }
        }, "t21");
 
    // 新建t22, t22會調(diào)用 x.isSyncB()
    Thread t22 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            y.isSyncA();
          }
        }, "t22");
 
 
    t21.start(); // 啟動(dòng)t21
    t22.start(); // 啟動(dòng)t22
  }
 
  public static void main(String[] args) {
    LockTest2 demo = new LockTest2();
 
    demo.test2();
  }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t21 : isSyncA
t22 : isSyncA
t21 : isSyncA
t22 : isSyncA
t21 : isSyncA
t22 : isSyncA
t21 : isSyncA
t22 : isSyncA
t21 : isSyncA
t22 : isSyncA

(3) 不能被同時(shí)訪問
因?yàn)閏SyncA()和cSyncB()都是static類型,x.cSyncA()相當(dāng)于Something.isSyncA(),y.cSyncB()相當(dāng)于Something.isSyncB(),因此它們共用一個(gè)同步鎖,不能被同時(shí)反問。

?
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
67
68
69
70
71
72
73
74
75
76
// LockTest3.java的源碼
class Something {
  public synchronized void isSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public synchronized void isSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
}
 
public class LockTest3 {
 
  Something x = new Something();
  Something y = new Something();
 
  // 比較(03) x.cSyncA()與y.cSyncB()
  private void test3() {
    // 新建t31, t31會調(diào)用 x.isSyncA()
    Thread t31 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            x.cSyncA();
          }
        }, "t31");
 
    // 新建t32, t32會調(diào)用 x.isSyncB()
    Thread t32 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            y.cSyncB();
          }
        }, "t32");
 
 
    t31.start(); // 啟動(dòng)t31
    t32.start(); // 啟動(dòng)t32
  }
 
  public static void main(String[] args) {
    LockTest3 demo = new LockTest3();
 
    demo.test3();
  }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t31 : cSyncA
t31 : cSyncA
t31 : cSyncA
t31 : cSyncA
t31 : cSyncA
t32 : cSyncB
t32 : cSyncB
t32 : cSyncB
t32 : cSyncB
t32 : cSyncB

(4) 可以被同時(shí)訪問
因?yàn)閕sSyncA()是實(shí)例方法,x.isSyncA()使用的是對象x的鎖;而cSyncA()是靜態(tài)方法,Something.cSyncA()可以理解對使用的是“類的鎖”。因此,它們是可以被同時(shí)訪問的。

?
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
67
68
69
70
71
72
73
74
75
76
// LockTest4.java的源碼
class Something {
  public synchronized void isSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public synchronized void isSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : isSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncA(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncA");
      }
    }catch (InterruptedException ie) {
    }
  }
  public static synchronized void cSyncB(){
    try {
      for (int i = 0; i < 5; i++) {
        Thread.sleep(100); // 休眠100ms
        System.out.println(Thread.currentThread().getName()+" : cSyncB");
      }
    }catch (InterruptedException ie) {
    }
  }
}
 
public class LockTest4 {
 
  Something x = new Something();
  Something y = new Something();
 
  // 比較(04) x.isSyncA()與Something.cSyncA()
  private void test4() {
    // 新建t41, t41會調(diào)用 x.isSyncA()
    Thread t41 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            x.isSyncA();
          }
        }, "t41");
 
    // 新建t42, t42會調(diào)用 x.isSyncB()
    Thread t42 = new Thread(
        new Runnable() {
          @Override
          public void run() {
            Something.cSyncA();
          }
        }, "t42");
 
 
    t41.start(); // 啟動(dòng)t41
    t42.start(); // 啟動(dòng)t42
  }
 
  public static void main(String[] args) {
    LockTest4 demo = new LockTest4();
 
    demo.test4();
  }
}

運(yùn)行結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
t41 : isSyncA
t42 : cSyncA
t41 : isSyncA
t42 : cSyncA
t41 : isSyncA
t42 : cSyncA
t41 : isSyncA
t42 : cSyncA
t41 : isSyncA
t42 : cSyncA

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 网站激情| 91精品国产乱码久久久久 | 欧美精品欧美 | 国产一级aa大片毛片 | 精品一区二区久久久久久久网精 | 99精品在线观看 | 欧美日韩国产成人在线观看 | 中文字幕免费播放 | 亚洲啪| 久久精品av| 欧美成人h版在线观看 | 国产亚洲精品久久久久久久软件 | 久久免费视频一区 | 日本一区二区免费在线播放 | 欧美黄色看 | 2021年无线乱码播放高清完整 | 日日草夜夜操 | 青青草华人在线 | 亚洲成人伊人 | 视频一区 在线 | 欧美精品免费一区二区三区 | 久久99国产精品免费网站 | 99久久久国产精品 | 成人偷拍片视频在线观看 | 在线播放亚洲视频 | 高清中文字幕在线 | 欧美性生活视频免费看 | 日韩在线播放第一页 | 免费人成在线播放 | 91香蕉影视 | 色就色 综合偷拍区91网 | 99久久婷婷国产综合精品青牛牛 | 欧美一级黄 | 竹内纱里奈55在线观看 | 91九色视频在线播放 | 国产免费人做人爱午夜视频 | 国产精品成人亚洲一区二区 | 久久久精品视频网站 | 黄网站在线播放视频免费观看 | 精品国产乱码久久久久久预案 | 欧美精品日日鲁夜夜添 |