public class SynchronizedTest { public synchronized void method1(){} public synchronized void method2(){} public static synchronized void method3(){} public static synchronized void method4(){} }
// 那么,有SynchronizedTest的两个实例a和b,对于一下的几个选项有哪些能被一个以上的线程同时访问呢? A. a.method1() vs a.method2() [x] B. a.method1() vs b.method1() [√] C. a.method3() vs b.method4() [x] D. a.method3() vs b.method3() [x] E. a.method1() vs a.method3() [√] // 正确答案是:B,E
class A{ public static int X; static{ System.out.println(Test.B); X = Test.B + 1; } } class C{ public static int Z; static{ System.out.println("C"); Z = Test.B + 1; } } public class Test { public static int B = A.X + 1; static{System.out.println(A.X);} public static void main(String[] args) { System.out.println(A.X+ "," +Test.B); int a = C.Z; } } //输出结果,解释见下面类加载的时机。 0 1 1,2 C
public class Parent { { System.out.println("Parent block"); } static{ System.out.println("Parent static block"); } public Parent(){ System.out.println("Parent construct method"); } } public class Son extends Parent { { System.out.println("son block"); } static{ System.out.println("son static block"); } public Son(){ System.out.println("son construct method"); } } public class Test { public static void main(String[] args) { new Son(); } } //输出结果 Parent static block son static block Parent block Parent construct method son block son construct method
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[];
static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h;
cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; }