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)代码.

举例:

 public class Thread9 {
private String a = "";
private String b = "";
private String c = ""; private void sync_fun1() throws InterruptedException {
synchronized (a.getClass()) {
int i = ;
while (i-- > ) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep();
} catch (InterruptedException ie) {
}
}
}
} private void sync_fun2() throws InterruptedException {
synchronized (b.getClass()) {
int i = ;
while (i-- > ) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep();
} catch (InterruptedException ie) {
}
}
}
} private synchronized void sync_fun3() {
int i = ;
while (i-- > ) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep();
} catch (InterruptedException ie) {
}
}
} public static void main(String args[]) {
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
new Thread9().sync_fun1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
new Thread9().sync_fun2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t2");
Thread t3 = new Thread(new Runnable() {
public void run() {
new Thread9().sync_fun3();
}
}, "not sync class"); t1.start();
t2.start();
t3.start();
}
}

结果

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

    

synchronized(5)修饰语句块之:synchronized(XXX.class)的更多相关文章

  1. synchronized(4)修饰语句块之:synchronized(this)

    synchronized(this) 此时,线程获得的是对象锁.例如: public class Thread8 extends Thread { public void sync_fun() { s ...

  2. synchronized(3)修饰语句块之:synchronized(一般对象)

    synchronized(一般对象) 一次只有一个线程进入该代码块.此时,线程获得的是成员锁.例如: public class Thread7 { private Object xlock = new ...

  3. synchronized(1)用法简介:修饰方法,修饰语句块

    注意: 同一个对象或方法在不同线程中才出现同步问题,不同对象在不同线程互相不干扰. synchronized方法有2种用法:修饰方法,修饰语句块 1.synchronized方法 是某个对象实例内,s ...

  4. java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较

    synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...

  5. synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较

    synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...

  6. 2.2synchronized同步语句块

    使用synchronized虽然能够避免不同步的现象出现,但是也会出现弊端,比如代码执行时间过长,那么其他线程就必须等待该线程执行完毕释放锁之后才能拿到锁. 面对这种问题可以使用同步代码块来解决. 2 ...

  7. java synchronized静态同步方法与非静态同步方法,同步语句块

    摘自:http://topmanopensource.iteye.com/blog/1738178 进行多线程编程,同步控制是非常重要的,而同步控制就涉及到了锁. 对代码进行同步控制我们可以选择同步方 ...

  8. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchronized关键字修饰)

    不多说,直接上干货! 扩展博客 牛客网Java刷题知识点之多线程同步的实现方法有哪些 为何要使用同步?      java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查 ...

  9. synchronized(修饰方法和代码块)

    synchronized(修饰方法和代码块) 1. 含义 synchronized 是同步锁,用来实现互斥同步. 在 Java 中,关键字 synchronized 可以保证在同一个时刻,只有一个线程 ...

随机推荐

  1. Android 自己定义UI文章汇总

    <Android ListView分类/分组效果 - 第一种实现方式> <Android ListView分类/分组效果 - 另外一种实现方式> <Android Lis ...

  2. C#数据库连接池 MySql SqlServer

    查阅了一天的资料来学习MySql数据库连接池,终于在一篇博文上找到了,自己也整理了一下,希望对大家有用处 1. 建立连接池 using MySql.Data.MySqlClient; using Sy ...

  3. STL vector的介绍(1)

    尝试下翻译STL里面的一些easy和算法.四级过了.六级刚考.顺便练练自己的英语水平.翻译的不好的地方请大神多多不吝赐教哈.方便我改正. 原来均来自:http://www.cplusplus.com/ ...

  4. 6 使用Ionic开发天气应用

    简介:本节课我们会制作一款天气应用,这款应用允许用户查看当前的天气情况.天气预报以及地点收藏,在模态框内显示日出和日落的数据,使用分页滚动面板显示天气信息,使用侧滑菜单实现导航. 6.1 项目配置 环 ...

  5. WebService(2)-XML系列之用Stax操作Xml

    源代码下载:链接: http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:利用Stax处理xml文档 一.读取xml 1.基于光标的查找 核心:X ...

  6. HDU 5274 Dylans loves tree 树链剖分+线段树

    Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...

  7. Adding Security to an Existing Website

    The procedure earlier in this article relies on using the Starter Site template as the basis for web ...

  8. XMU 1612 刘备闯三国之桃园结义 【二分】

    1612: 刘备闯三国之桃园结义 Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 181  Solved: 12[Submit][Status][We ...

  9. RabbitMQ的远程Web管理与监控工具

    RabbitMQ提供了完善的管理和监控工具,分management plugin 和 rabbitmqctl 两种类型的工具. 1.management plugin  rabbitmq-manage ...

  10. myeclipse 8.6安装SVN插件

    方法二: 安装subclipse, SVN 插件   1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigris.org,    2.从中解压出features与plug ...