【翻译十】java-固定锁和同步
Intrinsic Locks and Synchronization
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock. (The API specification often refers to this entity simply as a "monitor.") Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before relationships that are essential to visibility.
Every object has an intrinsic lock associated with it. By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them. A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock. As long as a thread owns an intrinsic lock, no other thread can acquire the same lock. The other thread will block when it attempts to acquire the lock.
When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquistion of the same lock.
Locks In Synchronized Methods
When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.
You might wonder what happens when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.
Synchronized Statements
Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock:
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
In this example, the addName method needs to synchronize changes to lastName and nameCount, but also needs to avoid synchronizing invocations of other objects' methods. (Invoking other objects' methods from synchronized code can create problems that are described in the section on Liveness.) Without synchronized statements, there would have to be a separate, unsynchronized method for the sole purpose of invoking nameList.add.
Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose, for example, class MsLunch has two instance fields, c1 and c2, that are never used together. All updates of these fields must be synchronized, but there's no reason to prevent an update of c1 from being interleaved with an update of c2 — and doing so reduces concurrency by creating unnecessary blocking. Instead of using synchronized methods or otherwise using the lock associated with this, we create two objects solely to provide locks.
public class MsLunch {
private long c1 = 0;
private long c2 = 0;
private Object lock1 = new Object();
private Object lock2 = new Object();
public void inc1() {
synchronized(lock1) {
c1++;
}
}
public void inc2() {
synchronized(lock2) {
c2++;
}
}
}
Use this idiom with extreme care. You must be absolutely sure that it really is safe to interleave access of the affected fields.
Reentrant Synchronization
Recall that a thread cannot acquire a lock owned by another thread. But a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization. This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock. Without reentrant synchronization, synchronized code would have to take many additional precautions to avoid having a thread cause itself to block.
译文:
固定锁和同步机制
同步机制是建立在被称为固定锁或者监控锁的内部实体之上的。(API 说明书通常指这种实体为”monitor”)固定锁在线程同步机制中扮演着两个角色:强制拥有一个对象进入的状态和创建确保基本可见的事先的关系。
每一个对象都有一个固定锁关联它。按照惯例,一个线程需要占用和进入一个对象域必须在进入之前事先捕获到这个对象的内部锁,让后当执行完就释放这个锁。一个线程拥有这个固定锁是指在他捕获到这个锁到释放这个固定锁的时间之间。在这个线程拥有这个固定锁的时候,没有其他的线程能够捕获到这个固定锁。
同步方法中的锁
当一个线程执行一个同步方法,它自动为这个方法的对象捕获到固定锁当返回时并释放它。即使这个返回是被一个未捕获到的异常引起的也会释放这个固定锁。
你可能会想到当一个静态的Synchronized方法被执行的情况,这是由于一个静态方法是关联到一个类的,而不是一个对象。在这种情况下,线程捕获到类对象关联到类的固定锁。因此静态类的访问控制是有别于任何其他类的实例的锁的访问控制的。
同步语句
另外一种创建同步方法的代码是同步语句。不像同步方法,同步语句必须指定提供固定锁的对象。
public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); }
在这个实例中,andName方法需要同步lastName和nameCount的改变,但是也需要避免同步请求其他对象的方法。(在同步的语句中执行其他对象的方法可能引起将会在Liveness中描述的问题。)除了同步语句,这里将会有一种分割,非同步的方法一唯一的目的调用nameList.add。
同步语句对细粒度的同步提高并发也是有用的。假如,MsLunch类有两个实例,c1和c2,并且它们不会在一起使用。所有对这些域的改变都应该是同步的,但是没有理由阻止c1和c2进行交叉的更新。而且这样做可以减少并发造成不必要的阻塞。我们创建了两个对象提供锁,而不是利用同步方法或其他关联到此的锁机制。
public class MsLunch {
private long c1 = 0;
private long c2 = 0;
private Object lock1 = new Object();
private Object lock2 = new Object();
public void inc1() {
synchronized(lock1) {
c1++;
}
} public void inc2() { synchronized(lock2) { c2++;
} } }
使用这个约定俗称的方法要格外小心。你必须肯定交叉访问的区域是绝对安全的。
重复进入同步
回想起一个线程不能获得另外一个线程占用的锁。但是一个线程能够获得它已经拥有的锁。允许一个线程获得相同的锁超过一次使重复进入线程成为可能。这描述了一种情况同步代码直接或者非直接执行一个也包含Synchronized的代码,并且两个代码集合用相同的锁。除了重复进入同步语句,同步代码必须采取许多措施预防它自己使它自己阻塞。
养眼是必须滴^^
【翻译十】java-固定锁和同步的更多相关文章
- JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是clas ...
- “全栈2019”Java多线程第十六章:同步synchronized关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10
关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...
- Java进阶(四十)Java类、变量、方法修饰符讲解
Java进阶(四十)Java类.变量.方法修饰符讲解 Java类修饰符 abstract: 将一个类声明为抽象类,没有实现的方法,需要子类提供方法实现. final: 将一个类生命为最终(即非继承类) ...
- Java中线程同步的理解 - 其实应该叫做Java线程排队
Java中线程同步的理解 我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread). 线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可 ...
- java两种同步机制的实现 synchronized和reentrantlock
java两种同步机制的实现 synchronized和reentrantlock 双11加保障过去一周,趁现在有空,写一点硬货,因为在进入阿里之后工作域的原因之前很多java知识点很少用,所以记录一下 ...
- Java中线程同步的理解
我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread). 线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源, ...
- 把Scheme翻译成Java和C++的工具
一.为什么要写这个工具? 公司内容有多个项目需要同一个功能,而这些项目中,有的是用Java的,有的是用C++的,同时由于某些现实条件限制,无法所有项目都调用统一的服务接口(如:可能运行在无网络的情况下 ...
- java中线程同步的理解(非常通俗易懂)
转载至:https://blog.csdn.net/u012179540/article/details/40685207 Java中线程同步的理解 我们可以在计算机上运行各种计算机软件程序.每一个运 ...
随机推荐
- intellij Idea快捷键
CTRL+ALT+O 优化导入的类和包 Alt + Center 导入类,实现接口 CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查找类中的方法或变 ...
- cocoapods for xcode怎么用
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
- 【云计算】docker三剑客如何支持分布式部署?
This blog will explain how to create multi-container application deployed on multiple hosts using Do ...
- block,inline和inline-block对比
总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...
- Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- Html空格字符代码:
Html空格字符代码: 为html空格字符代码,由“&+n+b+s+p+;”组成,记住最后一个分号不要忘记了.
- Appium+Robotframework实现Android应用的自动化测试-1:Appium在Windows中的安装
让我们开始在Windows中开始安装Appium吧,Appium在OS X中的具体安装后面的文章会介绍. 另外,官网上说先要装Node.js,还要装Apache Ant和Apache Maven,Gi ...
- centos6.5 iptables结合ipset批量屏蔽ip
安装ipset yum install ipset #创建ip地址集合 ipset create bansms hash:net 查找访问了“getVerificationCode”并且次数大于10次 ...
- Unity3D 给模型偏移纹理
给模型偏移纹理 using UnityEngine; using System.Collections; [RequireComponent(typeof(Renderer))] public cla ...
- Android开发者必备的42个链接
http://mobile.51cto.com/ahot-426035.htm Android开发者必备的42个链接 下面收集了42个帮助大家学习Android的内容链接,部分内容是面向初学者的,帮助 ...