0

It's possibile in java to have a class that don't have multiple instance between thread and classloader

for example, this is a simple singleton class that work with multiple thread

public class SynchronizedData {

    private static SynchronizedData instance;
    private volatile String data;

    private SynchronizedData() {

    }

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

    public synchronized String getData() {
        return data;
    }

    public synchronized void setData(String data) {
        this.data = data;
    }   
}

But don't work if is called by two different classloader.

Emax
  • 1,343
  • 2
  • 19
  • 30
  • i have already see that post and it don't resolve my problem – Emax Dec 20 '14 at 13:33
  • Using conventional singleton schemes, if you have multiple copies of a "singleton" class being loaded by multiple class loaders, you can have multiple instances of the singleton. This is because the same .class file loaded by two different class loaders is considered to be two totally different classes. – Hot Licks Dec 20 '14 at 13:40
  • @HotLicks i know of this problem, but how can i resolve? – Emax Dec 20 '14 at 13:48
  • There is one suggestion in the dupe. Basically you need some sort of global arbiter that *will not* be loaded twice. (How you enforce that, I don't know -- maybe a file in the file system.) – Hot Licks Dec 20 '14 at 13:48
  • You do understand, don't you, that the problems is not simply having multiple class loaders, but that you have more than one with the same .class in the loader's path. This is an unusual situation and will cause you problems even without singletons. – Hot Licks Dec 20 '14 at 15:23

0 Answers0