在学习多线程编程时,相信大家会遇到好多概念类的东西,对于这些概念的不准确理解会导致后面越学越糊涂,现将学习过程中遇到的概念整理到这篇博客上,一来记录学习点滴,二来也加深理解,如果有理解不准确的地方,希望大家指出。
参考与并发编程网 http://ifeve.com/java-concurrency-thread-directory/
public class ImmutableValue{ //该类没有提供setValue()方法,就没有提供改变自身状态的接口,这样的类就是线程安全的 private int value = 0; public ImmutableValue(int value){ this.value = value; } public int getValue(){ return this.value; } }//这个类是非线程安全的,并且引用了不可变的对象,但它暴露了访问不可变对象的接口,即add()方法,这样原先线程安全的类对象(currentValue)就变得不安全了public void Calculator{ private ImmutableValue currentValue = null; public ImmutableValue getValue(){ return currentValue; } public void setValue(ImmutableValue newValue){ this.currentValue = newValue; } public void add(int newValue){ this.currentValue = this.currentValue.add(newValue); } }
synchronized(lock){
your statement
}
//同步块1
synchronized(lock){
your statement1
}
//同步块2
synchronized(lock){
your statement2
}
原文:http://www.cnblogs.com/liuwaner118/p/4363919.html