如何编写单例来处理多个进程?可能吗? 例如,我的代码适用于Android中的远程服务。我怎样才能为此目的编写单例?
更新:
public enum Singleton {
INSTANCE;
int a = 0;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
void doFromFirstProcess(){
Singleton.INSTANCE.setA(1);
}
void doFromSecondProcess(){
Singleton.INSTANCE.getA(); //0
}
为什么不起作用?我的代码出了什么问题?
答案 0 :(得分:2)
不,单独的进程不共享任何内存。