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. Hashtable和HashMap类的区别

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  2. 2016年11月14日--SQL创建数据库、表-查、插、删、改

    --创建数据库(create database 数据库名)create database hq20161114go --使用选择数据库(use 数据库名)use hq20161114go --创建学生 ...

  3. WebApp的前端所遇问题

    2015年10月1日至10月25日,两个本科生和一位研究生以及一位老师组成四人团队,开发某教育前端项目 所遇问题总结以及分享: 一.主要应用软件:sublime_text HBuilder 初学者可以 ...

  4. 2.3---删除链表的结点,不提供头结点(CC150)

    这里,注意如果是尾结点,那么无解. public class Solution { public void deleteNode(ListNode node) { //利用李代桃僵 // // if( ...

  5. 调用pyxmpp库PyQt编程打包成exe文件出错

    pyxmpp库内resolver.py内getaddrinfo获取Openfire服务器地址出错

  6. MySQL 查看最大连接数, 当期连接数.

    查看最大连接数 select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME='MAX_CONN ...

  7. ubuntu下文件内容查找命令

    Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名 例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件 ...

  8. Java语言中几个常用的包

    Java采用包结构来组织和管理类和接口文件.本文介绍Java语言类库中几个常用的包,因为这几个包在软件开发与应用中经常需要用到,其中有些包是必要的.若是离开它,还真不能做事情了. 第一个包:java. ...

  9. js原生的url操作函数,及使用方法。(附:下边还有jquery对url里的中文解码函数)

    js原生的url操作函数,完善的. /*****************************/ /* 动态修改url */ /*****************************/ var ...

  10. Factor Combinations

    Factor Combinations Problem: Numbers can be regarded as product of its factors. For example, 8 = 2 x ...