博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Synchronized Methods
阅读量:5113 次
发布时间:2019-06-13

本文共 3627 字,大约阅读时间需要 12 分钟。

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.
java编程语句提供了2种基本的同步习语(idioms)。同步方法(synchronized methods)和同步语句(synchronized statements)。第二种更复杂,同步语句(synchronized statements)在下一章介绍。这一章是关于同步方法(synchronized methods)

To make a method synchronized, simply add the synchronized keyword to its declaration:
简单的在方法的声明上加上synchronized关键字就可以使方法同步
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:
如果count是SynchronizedCounter类的一个实例,使这三个方法同步有2个效果
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.
第一,同一个对象上的同步方法不可能交错执行。当一个线程(这里我们叫线程A)正在执行一个对象(假设叫对象B)的同步方法时,所有其他的线程直到第一个线程(线程A)中的(对象B的)同步方法执行完毕才回调用(B对象中的)同步方法块
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.
第二,当一个同步方法退出,它会自动与随后调用的相同对象中的同步方法建立一种happens-before关系。这保证了对象状态的改变对于其他线程是可见的。
注意构造方法(constructs)不能够被同步-在构造器(construct)上使用synchonized关键字是一种语法错误。同步构造器也没有任何意义,因为只有线程在创建对象时才会使用它

--------------------------------------------------------------------------------
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.
警告:一个正在构造的对象将在线程间共享,必须非常小心不要过早(prematurely)的泄露对象的引用。比如,假设你想维护一个叫instances包含每个类实例的List。你可能想在你的代码中增加下面的代码
instances.add(this);
但是其他的对象会在你对象构造完成前使用instances访问你正在构造的对象。
--------------------------------------------------------------------------------

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.

同步方法是一种简单的防止线程冲突(thread interference)和线程一致性错误(memory consistency errors)的策略:如果一个对象对多个对象可见。所有针对对象变量的读写都会通过同步方法。(一个重要的例外:final fields,在线程构造完毕后不能被更改,一旦对象被构造,能够通过非同步方法访问)。这种策略是有效的,但是会存在活性(liveness)问题,我们将在下章讨论这个

 

转载于:https://www.cnblogs.com/yuwenxing/archive/2012/05/24/2517032.html

你可能感兴趣的文章
如何判断主机是大端还是小端(字节序)
查看>>
Centos7 日志查看工具
查看>>
使用Xshell密钥认证机制远程登录Linux
查看>>
BZOJ2459 : [BeiJing2011]神秘好人
查看>>
Django入门示例之被解放的姜戈——01 初试天涯(安装及启动)
查看>>
OpenCV之响应鼠标(三):响应鼠标信息
查看>>
python7 数据类型的相互转化 字符编码
查看>>
Android 画图之 Matrix(一)
查看>>
React Native - 2 控件Flexbox
查看>>
前缀和
查看>>
Jquery插件汇集:
查看>>
Linux 启动、关闭、重启网络服务的两种方式
查看>>
List<T>列表通用过滤模块设计
查看>>
【模板】最小生成树
查看>>
设计模式之结构型模式
查看>>
修改navigationitem的title颜色字体阴影等属性
查看>>
前端开发中提到的“脚手架”到底指什么,CLI?gulp 和 gulp-cli有什么区别
查看>>
iis7规范URL及利用web.config进行重定向
查看>>
【Linux】入门篇 环境搭建
查看>>
poj2569
查看>>