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         /* init complete; allow other threads time to finish printing */
     64         Main.sleep(500);
     65 
     66         /* print all values */
     67         System.out.println("Fields (main thread): " +
     68             SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() +
     69             SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue());
     70     }
     71 
     72     static class FieldThread extends Thread {
     73         public void run() {
     74             /* allow class init to start */
     75             Main.sleep(200);
     76 
     77             /* print fields; should delay until class init completes */
     78             System.out.println("Fields (child thread): " +
     79                 SlowInit.FIELD0.getValue() + SlowInit.FIELD1.getValue() +
     80                 SlowInit.FIELD2.getValue() + SlowInit.FIELD3.getValue());
     81         }
     82     }
     83 
     84     static class MethodThread extends Thread {
     85         public void run() {
     86             /* allow class init to start */
     87             Main.sleep(400);
     88 
     89             /* use a method that shouldn't be accessible yet */
     90             SlowInit.printMsg("MethodThread message");
     91         }
     92     }
     93 }
     94