java锁实现原理:

http://blog.csdn.net/endlu/article/details/51249156

The synchronized keyword can be used to mark four different types of blocks:

  1. Instance methods
  2. Static methods
  3. Code blocks inside instance methods
  4. Code blocks inside static methods

Instance methods & Code blocks inside instance methods

Java实例方法同步是同步在拥有该方法的对象上

同步构造器中用括号括起来的对象叫做监视器对象

public class SyncBlockTest {

    public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(2);
final MySyncBlockClass syncBlockClass = new MySyncBlockClass();
es.submit(new Runnable() {
@Override
public void run() {
try {
syncBlockClass.mehtod1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
es.submit(new Runnable() {
@Override
public void run() {
try {
syncBlockClass.mehtod2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
es.shutdown();
} } class MySyncBlockClass { public synchronized void mehtod1() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
System.out.println(System.currentTimeMillis() + ":method1 run.");
} public synchronized void mehtod2() throws InterruptedException {
TimeUnit.SECONDS.sleep(4);
System.out.println(System.currentTimeMillis() + ":method2 run.");
}
} // method1较method2延迟了2000ms
// 1479350064132:method2 run.
// 1479350066132:method1 run.

实例方法同步

/**
* method1 与method2等效
* 同步构造器中用括号括起来的对象叫做监视器对象
*
* @throws InterruptedException
*/
public synchronized void mehtod1() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
System.out.println(System.currentTimeMillis() + ":method1 run.");
} public synchronized void mehtod2() throws InterruptedException {
synchronized (this) {
TimeUnit.SECONDS.sleep(4);
System.out.println(System.currentTimeMillis() + ":method2 run.");
}
}

实例方法中的同步块

Static methods & Code blocks inside static methods

静态方法的同步是指同步在该方法所在的类对象上。因为在Java虚拟机中一个类只能对应一个类对象,所以同时只允许一个线程执行同一个类中的静态同步方法。

public class SyncBlockTest {

    public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(2);
final MySyncBlockClass syncBlockClass1 = new MySyncBlockClass();
final MySyncBlockClass syncBlockClass2 = new MySyncBlockClass();
es.submit(new Runnable() {
@Override
public void run() {
try {
syncBlockClass1.mehtod1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
es.submit(new Runnable() {
@Override
public void run() {
try {
syncBlockClass2.mehtod2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
es.shutdown();
} } class MySyncBlockClass { public static synchronized void mehtod1() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
System.out.println(System.currentTimeMillis() + ":method1 run.");
} public static synchronized void mehtod2() throws InterruptedException {
TimeUnit.SECONDS.sleep(4);
System.out.println(System.currentTimeMillis() + ":method2 run.");
}
} // method1较method2延迟了3000ms
// 1479358310630:method1 run.
// 1479358314631:method2 run.

静态方法同步

/**
* method1 与method2等效
* 静态方法中的同步块
*
* @throws InterruptedException
*/
public static synchronized void mehtod1() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
System.out.println(System.currentTimeMillis() + ":method1 run.");
} public static synchronized void mehtod2() throws InterruptedException {
synchronized (MySyncBlockClass.class) {
TimeUnit.SECONDS.sleep(4);
System.out.println(System.currentTimeMillis() + ":method2 run.");
}
}

静态方法中的同步块

示例:

以下代码对应的Block关系如下:

synchronized(class)与 static synchronized 等效
public class SyncMethod2 {
private int value = 0;
private final Object mutex = new Object(); public synchronized int incAndGet0() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("static synchronized void incAndGet0");
return ++value;
} public int incAndGet1() {
synchronized(this){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("static synchronized void incAndGet1");
return ++value;
}
} public int incAndGet2() {
synchronized(SyncMethod.class){
++value;
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("static synchronized void incAndGet4");
}
return 0;
} public int incAndGet3() {
synchronized(mutex){
return ++value;
}
} public static synchronized void incAndGet4() {
try {
TimeUnit.SECONDS.sleep(4);
System.out.println("static synchronized void incAndGet4");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

各种同步场景

Java 同步代码块 - Synchronized Blocks的更多相关文章

  1. java多线层同时运行的解决,同步代码块synchronized

    /* 多个线层同时操作一个数据 会导制数据超出 同步代码块 synchronized(对像) { 需要同步的代码 } */ class Do7 { public static void main(St ...

  2. java 同步代码块与同步方法

    同步代码块 synchronized (obj) { // 代码块 } obj 为同步监视器,以上代码的含义为:线程开始执行同步代码块(中的代码)之前,必须先获得对同步监视器的锁定. 代码块中的代码是 ...

  3. java 多线程:线程通信-等待通知机制wait和notify方法;(同步代码块synchronized和while循环相互嵌套的差异);管道通信:PipedInputStream;PipedOutputStream;PipedWriter; PipedReader

    1.等待通知机制: 等待通知机制的原理和厨师与服务员的关系很相似: 1,厨师做完一道菜的时间不确定,所以厨师将菜品放到"菜品传递台"上的时间不确定 2,服务员什么时候可以取到菜,必 ...

  4. 彻底理解线程同步与同步代码块synchronized

    public class Demo { public static synchronized void fun1(){ } public synchronized void fun2(){ } pub ...

  5. 36. 解决线程问题方式一(同步代码块synchronized)

    解决线程问题: 方式一:同步代码块(synchronized) 语法: synchronized ("锁对象") {             //需要锁定的代码       }   ...

  6. JAVA之旅(十三)——线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this

    JAVA之旅(十三)--线程的安全性,synchronized关键字,多线程同步代码块,同步函数,同步函数的锁是this 我们继续上个篇幅接着讲线程的知识点 一.线程的安全性 当我们开启四个窗口(线程 ...

  7. Java基础8-多线程;同步代码块

    作业解析 利用白富美接口案例,土豪征婚使用匿名内部类对象实现. interface White{ public void white(); } interface Rich{ public void ...

  8. Java之同步代码块处理实现Runnable的线程安全问题

    /** * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式 * * 1.问题:卖票过程中,出现了重票.错票 -->出现了线程的安全问题 * 2.问题出现的原因:当某 ...

  9. Java 基础 线程的Runnable接口 /线程的同步方法 /同步代码块

    笔记: /**通过 Runnable接口来实现多线程 * 1. 创建一个实现runnable 接口的类 * 2. 在类中实现接口的run() 抽象方法 * 3. 创建一个runnable 接口实现类的 ...

随机推荐

  1. Ubuntu GNOME 16.10 Beta 1问世了!

    导读 Ubuntu GNOME 16.10操作系统已经进入研发周期一段时间了,今天终于可以下载Beta 1版本进行测试了.作为Ubuntu官方flavor之一,Ubuntu GNOME团队非常努力的整 ...

  2. 分享一个Visual Studio的背景插件,让堆码更富情趣

    忘记一件重要的事情,我使用的是VS 2012版,其他更高版本应该是可以找到的,以下版本就不清楚了.有可能找不到,见谅,也不是我开发的,只是偶尔碰到,拿出来让大家知道. 上周某日,新生命群里面还是一如既 ...

  3. canvas的简单圆形进度条

    window.onload = function(){ function arc(canvas,number){ var canvas = document.getElementById(canvas ...

  4. CentOS7 词典

    goldendict sudo yum install goldendict打开goldendict,阅读welcome,添加本地词典,在http://abloz.com/huzheng/stardi ...

  5. SQLite入门语句之HAVING和DISTINCT

    一.SQLite入门语句之HAVING HAVING 子句允许指定条件来过滤将出现在最终结果中的分组结果. WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建 ...

  6. 教你分分钟开发一个属于自己的python模块(一)——能够直接在浏览器打印的方法

    曾经,用惯了python print命令的人,惊叹于python语法的精简:后来,用过了tornado.django等web开发框架,不得不佩服当初开发这些框架的人们.于是,我们开始使用它们的框架== ...

  7. 【Java EE 学习 28 下】【Oracle面试题2道】【Oracle练习题3道】

    一.已知程序和数据 create table test1 (id int primary key, name ), money int); ,); ,); ,); ,); 要求根据下图写出相应的sql ...

  8. OGG-01820 Could not enable workspace

    状况: OGG replicat进程abend了,查看report显示如下问题: 2016-11-01 16:11:47  ERROR   OGG-01820  Could not enable wo ...

  9. C语言中字符串结束符'\0'

    转自:http://www.cnblogs.com/kaituorensheng/archive/2013/12/09/3464462.html 本质 '\0'就是8位的00000000,因为字符类型 ...

  10. 在 windows 环境下安装 redislive

    这是一篇在 windows 环境下安装 redislive 的教程! 项目地址:https://github.com/nkrode/RedisLive 配置文档:http://www.nkrode.c ...