对象中的静态属性是否在多线程中共享
//1.一个对象中的静态属性是否在多线程中共享
// 如下:
class test {
public static ArrayList arr = new ArrayList();
public static StringBuffer buf = new StringBuffer();
public static int i = 0;
public synchronized static void add() {
i++;
buf.append(i);
}
public static void String() {
System.out.print(buf.toString());
}
}
//2.若是共享 ,可不可以把单态中的方法改成静态方法.
// 如下:
class AAA {
private static AAA a = null;
private static StringBuffer buf = new StringBuffer();
private AAA() {
}
public static synchronized AAA getInstance() {
if (a == null) {
a = new AAA();
}
return a;
}
//这里若改成静态方法,会不会影响(因为我看单态对象中的方法都不是静态方法)
public void add(int i ) {
buf.append(i);
}
}
//可否改成
class BBB{
private static StringBuffer buf = new StringBuffer();
public static void add(int i) {
buf.append(i);
}
}
//3.在多线程环境下,垃圾回收线程会在什么时回收这个含有静态属性的对象.