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. vs2012+qt5.2.0环境搭建

    1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit(VS 2012, 579 MB) 和 Visual Studio Add-in 1.2.2for Qt5 注意: ...

  2. 28 GroupSock(NetAddress)——live555源码阅读(四)网络

    28 GroupSock(NetAddress)——live555源码阅读(四)网络 28 GroupSock(NetAddress)——live555源码阅读(四)网络 简介 1) NetAddre ...

  3. ndk学习19: 使用Eclipse调试so

    1.  设置调试选项 在AndroidManifest文件加入允许调试 android:debuggable="true"   此时编译项目会多出: 2.  配置调试代码 把需要调 ...

  4. phpcms导航中添加内部链接

    phpcms中栏目有3中类型 1.普通栏目 2.单网页 3.外部链接 其中如果想添加本站的内部链接,可以使用3,然后在添加链接的地方填入剩下的地址即可(需要以/开头) 如: /index.php?m= ...

  5. kendoUI grid 过滤时出错:TypeError toLowerCase is not a function

    错误原因:类型不一致. 有些过滤类型为字符串,有些为整型时.

  6. Unity3d《Shader篇》法线贴图

    效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...

  7. FastReport中文网

    FastReport中文网 http://www.fastreportcn.com/Article/2.html

  8. Debian8.3.0下安装Odoo8.0步骤

    Debian8.3.0下安装Odoo8.0的方法 假设你已经安装好了Debian 系统,使用root帐号执行如下命令 # apt-get update && apt-get upgra ...

  9. 【XLL API 函数】xlAbort

    C API 中有 15个 Excel 回调函数只能使用 Excel4.Excel4v.Excel12.Excel12v 函数调用(或间接的使用框架函数 Excel 或 Excel12f 调用).也就是 ...

  10. php单例模式的研究

    几个关键点: 1,对象P应该可以被系统中的任何对象使用 2,对象P不应该被存储在会被覆写的全局变量总 3,系统中不应该超过一个P对象,也就是说,Y对象可以设置P对象的一个属性,而Z对象不需要通过其他对 ...