synchronized同步以及双重检索
一、synchronized同步参考
两个线程同时执行会出错,那么最简单的方法是让CPU执行完一个线程,再执行另一个线程,那么Java中给出了一个非常简单的解决办法,【synchronized】:是一种同步锁。简单解释一下:就是synchronized修饰的代码,同时只能有一个线程执行,即执行完一个线程,再执行另一个,其它需要执行的线程都要排队 。
synchronized是Java中的关键字,是一种同步锁。它修饰的对象有以下几种:
1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码块的对象;
2. 修饰一个方法,被修饰的方法称为同步方法,其作用的范围是整个方法,作用的对象是调用这个方法的对象;
3. 修改一个静态的方法,其作用的范围是整个静态方法,作用的对象是这个类的所有对象;
4. 修改一个类,其作用的范围是synchronized后面括号括起来的部分,作用主的对象是这个类的所有对象。
注意:在定义接口方法时不能使用synchronized关键字;构造方法不能使用synchronized关键字,但可以使用synchronized代码块来进行同步。
1、一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞。
public void test() {
synchronized (this){
......
}
}
2、当一个线程访问对象的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该对象中的非synchronized(this)同步代码块。
TestThread testThread = new TestThread();
Thread thread1 = new Thread(testThread, "testThread1");
Thread thread2 = new Thread(testThread, "testThread2");
thread1.start();
thread2.start();
//synchronized代码块
public void test1() {
synchronized(this) {
......
}
}
//非synchronized代码块
public void test2() {
......
}
//执行方法
public void run() {
String threadName = Thread.currentThread().getName();
if (threadName.equals("testThread1")) {
test1();
} else if (threadName.equals("testThread2")) {
test2();
}
}
3、修饰一个方法
synchronized修饰一个方法很简单,就是在方法的前面加synchronized,public synchronized void method(){}; synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起来的范围,而修饰方法范围是整个函数。
public synchronized void run() {
......
}
4、修饰一个静态的方法
public synchronized static void method() {
......
}
5、修饰一个类
class ClassName {
public void method() {
synchronized(ClassName.class) {
......
}
}
}
二、双重检索参考
1、单例模式
public class Singleton {
private static Singleton singleton;
private Singleton() {
}
public Singleton getInstance() {
if (null == singleton) {
singleton= new Singleton();
}
return singleton;
}
}
这样写在多线程的情况下,会导致singleton
有多个实例,完全违背了单例的初衷。
2、出现这种情况,第一反应就是加锁
public class Singleton {
private static Singleton singleton;
private Singleton() {
}
public synchronized Singleton getInstance() {
if (null == singleton) {
singleton= new Singleton();
}
return singleton;
}
}
这样虽然解决了问题,但是因为用到了synchronized
,会导致很大的性能开销,并且加锁其实只需要在第一次初始化的时候用到,之后的调用都没必要再进行加锁。
3、之后会想到双重加锁:双重检查锁是对上述问题的一种优化。先判断对象是否已经被初始化,再决定要不要加锁。
public class Singleton {
private static Singleton singleton;
private Singleton() {
}
public Singleton getInstance() {
if (null == singleton) {
synchronized (Singleton.class) {
if (null == singleton) {
singleton= new Singleton();//error
}
}
}
return singleton;
}
}
如果这样写,运行顺序就成了:
- 检查变量是否被初始化(不去获得锁),如果已被初始化则立即返回。
- 获取锁。
- 再次检查变量是否已经被初始化,如果还没被初始化就初始化一个对象。
执行双重检查是因为,如果多个线程同时了通过了第一次检查,并且其中一个线程首先通过了第二次检查并实例化了对象,那么剩余通过了第一次检查的线程就不会再去实例化对象。
这样,除了初始化的时候会出现加锁的情况,后续的所有调用都会避免加锁而直接返回,解决了性能消耗的问题。
隐患
上述写法看似解决了问题,但是有个很大的隐患。实例化对象的那行代码(标记为error的那行),实际上可以分解成以下三个步骤:
- 分配内存空间
- 初始化对象
- 将对象指向刚分配的内存空间
但是有些编译器为了性能的原因,可能会将第二步和第三步进行重排序,顺序就成了:
- 分配内存空间
- 将对象指向刚分配的内存空间
- 初始化对象
为了解决上述问题,需要在singleton
前加入关键字volatile
。使用了volatile关键字后,重排序被禁止,所有的写(write)操作都将发生在读(read)操作之前。
public class Singleton {
private volatile static Singleton singleton;
private Singleton() {
}
public Singleton getInstance() {
if (null == singleton) {
synchronized (Singleton.class) {
if (null == singleton) {
singleton= new Singleton();
}
}
}
return singleton;
}
}
这样双重检查锁就可以完美工作了。