Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 public class Main {
     18     public static void main(String[] args) {
     19         checkExceptions();
     20         checkTiming();
     21     }
     22 
     23     public static void sleep(int msec) {
     24         try {
     25             Thread.sleep(msec);
     26         } catch (InterruptedException ie) {
     27             System.err.println("sleep interrupted");
     28         }
     29     }
     30 
     31     static void checkExceptions() {
     32         try {
     33             System.out.println(PartialInit.FIELD0);
     34             System.err.println("Construction of PartialInit succeeded unexpectedly");
     35         } catch (ExceptionInInitializerError eiie) {
     36             System.out.println("Got expected EIIE for FIELD0");
     37         }
     38 
     39         try {
     40             System.out.println(PartialInit.FIELD0);
     41             System.err.println("Load of FIELD0 succeeded unexpectedly");
     42         } catch (NoClassDefFoundError ncdfe) {
     43             System.out.println("Got expected NCDFE for FIELD0");
     44         }
     45         try {
     46             System.out.println(PartialInit.FIELD1);
     47             System.err.println("Load of FIELD1 succeeded unexpectedly");
     48         } catch (NoClassDefFoundError ncdfe) {
     49             System.out.println("Got expected NCDFE for FIELD1");
     50         }
     51     }
     52 
     53     static void checkTiming() {
     54         FieldThread fieldThread = new FieldThread();
     55         MethodThread methodThread = new MethodThread();
     56 
     57         fieldThread.start();
     58         methodThread.start();
     59 
     60         /* start class init */
     61         IntHolder zero = SlowInit.FIELD0;
     62 
     63         /* wait for children to complete */
     64         try {
     65             fieldThread.join();
     66             methodThread.join();
     67         } catch (InterruptedException ie) {
     68             System.err.println(ie);
     69         }
     70 
     71         /* print all values */
     72         System.out.println("Fields (main thread): " +
     73             SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() +
     74             SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue());
     75     }
     76 
     77     static class FieldThread extends Thread {
     78         public void run() {
     79             /* allow class init to start */
     80             Main.sleep(200);
     81 
     82             /* collect fields; should delay until class init completes */
     83             int field0, field1, field2, field3;
     84             field0 = SlowInit.FIELD0.getValue();
     85             field1 = SlowInit.FIELD1.getValue();
     86             field2 = SlowInit.FIELD2.getValue();
     87             field3 = SlowInit.FIELD3.getValue();
     88 
     89             /* let MethodThread print first */
     90             Main.sleep(400);
     91             System.out.println("Fields (child thread): " +
     92                 field0 + field1 + field2 + field3);
     93         }
     94     }
     95 
     96     static class MethodThread extends Thread {
     97         public void run() {
     98             /* allow class init to start */
     99             Main.sleep(200);
    100 
    101             /* use a method that shouldn't be accessible yet */
    102             SlowInit.printMsg("MethodThread message");
    103         }
    104     }
    105 }
    106