1 /* 2 * Copyright (C) 2020 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 import java.util.concurrent.atomic.AtomicInteger; 17 18 public class Main { 19 20 private static class LazyGrandChildThread implements Runnable { 21 @Override run()22 public void run() {} 23 } 24 25 private static class ChildThread implements Runnable { 26 @Override run()27 public void run() { 28 // Allocate memory forcing GCs and fork children. 29 for (int i = 0; i < 100; ++i) { 30 int [][] a = new int[10][]; 31 for (int j = 0; j < 10; ++j) { 32 a[j] = new int[50000 * j + 20]; 33 a[j][17] = 1; 34 } 35 Thread t = new Thread(new LazyGrandChildThread()); 36 t.start(); 37 int sum = 0; 38 // Make it hard to optimize out the arrays. 39 for (int j = 0; j < 10; ++j) { 40 sum += a[j][16] /* = 0 */ + a[j][17] /* = 1 */; 41 } 42 if (sum != 10) { 43 System.out.println("Bad result! Was " + sum); 44 } 45 try { 46 t.join(); 47 } catch (InterruptedException e) { 48 System.out.println("Interrupted by " + e); 49 } 50 } 51 System.out.println("Child finished"); 52 } 53 } 54 main(String[] args)55 public static void main(String[] args) { 56 System.out.println("Main Started"); 57 new Thread(new ChildThread()).start(); 58 System.out.println("Main Finished"); 59 } 60 } 61