synchronized(5)修饰语句块之:synchronized(XXX.class)

synchronized(XXX.class)有两种写法

  synchronized(XXX.class)或者synchronized(obj.getClass())

  • Class也是一个类xxx.class和obj.getClass()得到的是Class的一个实例对象.
  • 比如String.class是Class类的一个实例,Object.class也是Class的一个实例……。
  • 至于XX.class显然是一个Class的实例,而不是一个类。
  • synchronized (XXX.class)和synchronized(this)这样的语句在语法上是同一类型的,它们本质上是synchronized(对象),只不过锁的是Class的一个实例xxx.class。
  •  synchronized(XXX.class)会阻塞后面返回相同class的synchronized(XXX.class)代码.

举例:

 1 public class Thread9 {
 2     private String a = "123";
 3     private String b = "321";
 4     private String c = "456";
 5 
 6     private void sync_fun1() throws InterruptedException {
 7         synchronized (a.getClass()) {
 8             int i = 5;
 9             while (i-- > 0) {
10                 System.out.println(Thread.currentThread().getName() + " : " + i);
11                 try {
12                     Thread.sleep(500);
13                 } catch (InterruptedException ie) {
14                 }
15             }
16         }
17     }
18 
19     private void sync_fun2() throws InterruptedException {
20         synchronized (b.getClass()) {
21             int i = 5;
22             while (i-- > 0) {
23                 System.out.println(Thread.currentThread().getName() + " : " + i);
24                 try {
25                     Thread.sleep(500);
26                 } catch (InterruptedException ie) {
27                 }
28             }
29         }
30     }
31 
32     private synchronized void sync_fun3() {
33         int i = 5;
34         while (i-- > 0) {
35             System.out.println(Thread.currentThread().getName() + " : " + i);
36             try {
37                 Thread.sleep(500);
38             } catch (InterruptedException ie) {
39             }
40         }
41     }
42 
43     public static void main(String args[]) {
44         Thread t1 = new Thread(new Runnable() {
45             public void run() {
46                 try {
47                     new Thread9().sync_fun1();
48                 } catch (InterruptedException e) {
49                     e.printStackTrace();
50                 }
51             }
52         }, "t1");
53         Thread t2 = new Thread(new Runnable() {
54             public void run() {
55                 try {
56                     new Thread9().sync_fun2();
57                 } catch (InterruptedException e) {
58                     e.printStackTrace();
59                 }
60             }
61         }, "t2");
62         Thread t3 = new Thread(new Runnable() {
63             public void run() {
64                 new Thread9().sync_fun3();
65             }
66         }, "not sync class");
67 
68         t1.start();
69         t2.start();
70         t3.start();
71     }
72 }

结果

t1 : 4
not sync class : 4
t1 : 3
not sync class : 3
t1 : 2
not sync class : 2
t1 : 1
not sync class : 1
t1 : 0
not sync class : 0
t2 : 4
t2 : 3
t2 : 2
t2 : 1
t2 : 0

 

 

 

    

转载于:https://www.cnblogs.com/sjjg/p/4589430.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值