简体   繁体   English

单例方法线程安全

[英]Singleton methods thread safe

I have a question Singleton pattern and threads. 我有一个问题,单例模式和线程。 Implementation is like this. 实现是这样的。

public class Singleton {
    private static final Singleton instance = new Singleton();

    private SomeClass someField;
    // and  another private fields

    private Singleton() {
        someField = new SomeClass(some args);
        // init another private fields
    }

    public Singleton getInstance() {
        return instance;
    }

    public void operation() {
        //some operations
        someField.method();
    }
}

(Sorry I can not provide real example.) The question is next: is method operation() thread safe? (很抱歉,我无法提供真实的示例。)接下来的问题是:方法operation()线程安全吗?

We have no idea whether it's safe or not - we don't know what someField.method() does. 我们不知道它是否安全-我们不知道someField.method()作用。

I would strongly encourage you to make someField a final field, as if the singleton needs to mutate state then it's definitely not thread-safe without extra synchronization. 我强烈建议您将someFieldfinal字段,就好像单例需要更改状态一样,那么如果没有额外的同步它绝对不是线程安全的。 If SomeClass itself is immutable and thread-safe, then you shouldn't need any other synchronization - but otherwise, you will. 如果SomeClass本身是不可变的并且是线程安全的,则您不需要任何其他同步-否则,您将需要。

Basically, there's nothing "magically thread-safe" about a singleton. 基本上,单例没有“魔术般线程安全”的特性。 It's just a single instance which multiple threads will have access to via the static getInstance() method. 它只是一个实例,多个线程将通过静态getInstance()方法访问。 If the class is thread-safe, it's thread-safe regardless of whether it's a singleton - and if it's not thread-safe, then making it a singleton won't do anything to help that. 如果该类是线程安全的,则无论它是否是单例的,它都是线程安全的;并且,如果它不是线程安全的,那么将其设置为单例也无济于事。

The thread-safety of someField.method(); someField.method();的线程安全性 depends on what it's actually doing. 取决于实际操作。 If it's modifying state that is shared among multiple threads then it's not thread-safe. 如果它是在多个线程之间共享的修改状态,则它不是线程安全的。 If not, it might be thread-safe. 如果不是,则可能是线程安全的。 But in general it should not be assumed to be thread-safe. 但通常不应假定它是线程安全的。 I can't say more without the code. 没有代码,我不能说更多。

the answer is not thread safe as answered above. 答案不是上面回答的线程安全。 This can be test as the code below ! 可以作为下面的代码进行测试!


public class TestSingleton {
public static void main(String[] args) throws Exception {
    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (int j = 0; j < 100000; j++) {
        pool.submit(new Thread() {
            public void run() {

                Singleton.get().add();
            }
        });
    }
    pool.shutdownNow();
    System.out.println(Singleton.get().getcnt());
}

} }

class Singleton {
private static Singleton singleton = new Singleton();

int cnt = 0;

private Singleton() {}

public static Singleton get() {
    return singleton;
}

public void add() {
    cnt++;
}

public int getcnt() {
    return cnt;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM