Synchronized Methods

  The Java programming language provides two basic synchronization idioms: synchronized methods and synchronized statements. The more complex of the two, synchronized statements, are described in the next section. This section is about synchronized methods.

  To make a method synchronized, simply add the synchronized keyword to its declaration:

public class SynchronizedCounter {
private int c = 0;

public synchronized void increment() {
c++;
}

public synchronized void decrement() {
c--;
}

public synchronized int value() {
return c;
}
}
  If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:

  First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
  Note that constructors cannot be synchronized — using the synchronized keyword with a constructor is a syntax error. Synchronizing constructors doesn't make sense, because only the thread that creates an object should have access to it while it is being constructed.

  Warning: When constructing an object that will be shared between threads, be very careful that a reference to the object does not "leak" prematurely. For example, suppose you want to maintain a List called instances containing every instance of class. You might be tempted to add the following line to your constructor:
  instances.add(this);
  But then other threads can use instances to access the object before construction of the object is complete.
  Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed) This strategy is effective, but can present problems with liveness, as we'll see later in this lesson.

译文:

同步方法
  Java语句提供了两种基本的synchronization方式:synchronized方法和synchronized语句。这两种方法比较复杂的,synchronized语句,将在下一节中进行描述。这一节描述的是synchronized方法。
  为了写一个synchronized方法,只要在申明的时候加synchronized关键字就行了。

 public class SynchronizedCounter {
private int c = 0; public synchronized void increment() {
c++;
} public synchronized void decrement() {
c--;
} public synchronized int value() {
return c;
}
}

  如果count是SynchronizedCounter的一个实例,那么使这些方法成为synchronized方法有两个效果:

  第一,不可能使两个调用synchronized的方法中有相同的对象出现交叉的现象。当一个线程作为一个对象正在执行synchronized的方法的时候,其他的线程请求执行同一个对象的synchronized方法的时候会出现挂起知道第一个线程被这个对象执行完毕。
  第二,当一个synchronized方法存在的时候,它会自动的生成一个事先的关系在请求一个后来的这个对象的synchronized方法.这保证了这种改变保证其他所有线程都是可见的。
  注意构造函数不能用作synchronized方法,对构造函数添加synchronized关键字是一种语法错误。构造函数的同步是讲不通的,因为当它正在被构造的时候,只有线程创建对象的时候才应该进入它。
  警告:当构造一个将会被多个线程共享的对象的时候,注意对一种对象的引用不存在过早的泄露。例如,假如你想维护一个包好每个实例的类的链表调用的实例。你应该增加临时的一行在下面的构造函数中。
  instances.add(this);
  但是,其他的线程可以在这个对象的构造完成之前使用这个实例。
  Synchronized方法是一种简单的策略能够组织线程冲突和内存一致性错误:假如一个对象能够被其他的线程可以看见,所有的读和写的变量都通过synchronized方法处理。(一个重要的异常:常量域,能够被一个已经生成构造函数的对象所修改)这种策略是有效的,但是,能够引起其他问题出现,我们在这个课程的以后可能会看到。

养眼是必须滴^^

【翻译九】java-同步方法的更多相关文章

  1. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  2. JUnit单元测试教程(翻译自Java Code Geeks)

    JUnit单元测试教程--终极指南 JUnit单元测试教程终极指南 说明 单元测试简介 1 什么是单元测试 2 测试覆盖 3 Java中的单元测试 JUnit简介 1 使用Eclipse实现简单JUn ...

  3. Java进阶(三十九)Java集合类的排序,查找,替换操作

    Java进阶(三十九)Java集合类的排序,查找,替换操作 前言 在Java方向校招过程中,经常会遇到将输入转换为数组的情况,而我们通常使用ArrayList来表示动态数组.获取到ArrayList对 ...

  4. 把Scheme翻译成Java和C++的工具

    一.为什么要写这个工具? 公司内容有多个项目需要同一个功能,而这些项目中,有的是用Java的,有的是用C++的,同时由于某些现实条件限制,无法所有项目都调用统一的服务接口(如:可能运行在无网络的情况下 ...

  5. [翻译]现代java开发指南 第三部分

    现代java开发指南 第三部分 第三部分:Web开发 第一部分,第二部分,第三部分 =========================== 欢迎来到现代 Java 开发指南第三部分.在第一部分中,我们 ...

  6. JavaScript翻译成Java

    这两天公司有一个需求,将一段加密的JavaScript代码转换为JAVA版. JavaScript中的某一段代码: 前期查看了整个JavaScript代码,发现代码中,方法里面嵌套方法,各种不合规的变 ...

  7. 【Medium翻译】Java抽象类有什么用?

    今天安利一个网站,其实很多朋友应该早就知道了,我之前ARTS打卡,英文文档的 很多出处就来自于这个网站,叫 「Medium」. 这个网站需要一定的技术去访问,但是为什么说他好呢,因为他号称全球最大的高 ...

  8. Java同步方法:synchronized到底锁住了谁?

    目录 前言 同步方法 类的成员方法 类的静态方法 同步代码块 总结 其他同步方法 参考资料 前言 相信不少同学在上完Java课后,对于线程同步部分的实战,都会感到不知其然. 比如上课做实验的时候,按着 ...

  9. 菜鸡的Java笔记 第十九 - java 继承

    继承性的主要目的,继承的实现,继承的限制                继承是面向对象中的第二大主要特点,其核心的本质在于:可以将父类的功能一直沿用下去                为什么需要继承? ...

随机推荐

  1. OpenCv haar+SVM训练的xml检测人头位置

    注意:opencv-2.4.10 #include "stdio.h"#include "string.h"#include "iostream&qu ...

  2. BZOJ 4236: JOIOJI

    Description 给出一个字符串,只包含3个字母,询问最长的一个子串,3个字母出现次数相同. Sol map. 如果一个子串满足条件,那么它端点处的三个字母的个数两两差值都是一样的,直接存个状态 ...

  3. IOI2015 Boxes

    Description 给出一个环形,n个点,每次只能访问k个点,求最短距离. Sol 贪心. CCF的题解. 首先只会最多走一趟环形,根据抽屉原理,如果一边不足k个才会到另一边,所以对于第二次以上的 ...

  4. hdu1054 树形dp&&二分图

    B - Strategic Game Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. adb 服务端口2037被占,导致adb和appium无法工作

    症状1: 命令行运行 adb 相关命令,提示如下: adb server is out of date. killing...ADB server didn't ACK* failed to star ...

  6. Appium+Robotframework实现Android应用的自动化测试-2:Windows中启动Appium和模拟器

    一.启动Appium 安装好了之后,在桌面或者菜单中找到Appium,分别双击或点击打开Appium.exe,如果一切正常,接着会出现一个Appium启动后的界面窗口,如下图所示. 1.1 Andro ...

  7. 【转】Git如何Check Out出指定文件或者文件夹

    [转]Git如何Check Out出指定文件或者文件夹http://www.handaoliang.com/a/20140506/195406.html 在进行项目开发的时候,有时候会有这样的需求那就 ...

  8. Javascript 面向对象

    面向对象语言的标识:类的概念. ECMAScript中没有类的概念,因此他的对象与基于类的语言中的对象有所不同. ECMA-262把对象定义为:无序属性的集合,其属性可以包含基本值.对象.函数.我们可 ...

  9. 项目管理工具~Jira

    作用:工程管理 提交BUG 描述,截图,记录BUG ID 自定义DashBoard 添加Gadget 自定义布局 统计要素 TimeSheet 1.组内人力使用分布 2.员工工作量 Jira 过滤器设 ...

  10. cmd命令查看局域网内计算机信息

    ping [计算机名] ping -a [IP] nbtstat -a [IP] net view arp -a nslookup www.baidu.com 查看当前dns地址 tracert [I ...