Java单例和同步 [英] Java Singleton and Synchronization

查看:128
本文介绍了Java单例和同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请澄清我对Singleton和多线程的疑问:

Please clarify my queries regarding Singleton and Multithreading:


  • 在多线程$ b中用Java实现Singleton的最佳方法是什么? $ b environment?

  • 当多个线程同时尝试访问 getInstance()
    方法时会发生什么?

  • 我们可以制作单身人士的 getInstance() 已同步

  • 使用Singleton类时是否真的需要同步?

  • What is the best way to implement Singleton in Java, in a multithreaded environment?
  • What happens when multiple threads try to access getInstance() method at the same time?
  • Can we make singleton's getInstance() synchronized?
  • Is synchronization really needed, when using Singleton classes?

推荐答案

是, 有必要。有几种方法可以用来通过延迟初始化来实现线程安全:

Yes, it is necessary. There are several methods you can use to achieve thread safety with lazy initialization:

严酷的同步:

private static YourObject instance;

public static synchronized YourObject getInstance() {
    if (instance == null) {
        instance = new YourObject();
    }
    return instance;
}

此解决方案要求每个线程同步实际上只需要前几个。

This solution requires that every thread be synchronized when in reality only the first few need to be.

双重检查同步

private static final Object lock = new Object();
private static volatile YourObject instance;

public static YourObject getInstance() {
    YourObject r = instance;
    if (r == null) {
        synchronized (lock) {    // While we were waiting for the lock, another 
            r = instance;        // thread may have instantiated the object.
            if (r == null) {  
                r = new YourObject();
                instance = r;
            }
        }
    }
    return r;
}

此解决方案确保只有尝试获取单身人士的前几个线程才有通过获取锁定的过程。

This solution ensures that only the first few threads that try to acquire your singleton have to go through the process of acquiring the lock.

按需初始化

private static class InstanceHolder {
    private static final YourObject instance = new YourObject();
}

public static YourObject getInstance() {
    return InstanceHolder.instance;
}

此解决方案利用Java内存模型保证类初始化以确保线程安全。每个类只能加载一次,只有在需要时才会加载。这意味着第一次调用 getInstance ,将加载 InstanceHolder 并且 instance ,由于这是由 ClassLoader s控制的,因此无需进行其他同步。

This solution takes advantage of the Java memory model's guarantees about class initialization to ensure thread safety. Each class can only be loaded once, and it will only be loaded when it is needed. That means that the first time getInstance is called, InstanceHolder will be loaded and instance will be created, and since this is controlled by ClassLoaders, no additional synchronization is necessary.

这篇关于Java单例和同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆