1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.net.URI;
  25 import java.nio.file.FileSystem;
  26 import java.nio.file.FileSystems;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.util.List;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 /*
  34  * jimage shared open testing.
  35  * @test
  36  * @summary Test to see if thread interrupt handling interferes with other threads.
  37  * @build JImageOpenTest.java
  38  * @run main/othervm -Djdk.image.map.all=false JImageOpenTest
  39  */
  40 public class JImageOpenTest {
  41     private static final int NTHREADS = 10;
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         final FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
  46         final Path root = fs.getPath("/modules");
  47 
  48         final List<String> names = Files.walk(root)
  49                 .filter(p -> p.getNameCount() > 2)
  50                 .filter(p -> ModuleLayer.boot().findModule(p.getName(1).toString()).isPresent())
  51                 .map(p -> p.subpath(2, p.getNameCount()))
  52                 .map(p -> p.toString())
  53                 .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
  54                 .collect(Collectors.toList());
  55 
  56         Runnable r = new Runnable() {
  57             @Override
  58             public void run() {
  59                 names.forEach(name -> {
  60                     String cn = name.substring(0, name.length() - 6).replace('/', '.');
  61                     try {
  62                         Class.forName(cn, false, ClassLoader.getSystemClassLoader());
  63                     } catch (Exception ex) {
  64                         System.err.println(Thread.currentThread() + " " + ex.getClass());
  65                     }
  66                 });
  67             }
  68         };
  69 
  70         Thread[] threads = new Thread[NTHREADS];
  71 
  72         for (int i = 0; i < NTHREADS; i++) {
  73             Thread thread =  new Thread(r);
  74             threads[i] = thread;
  75             thread.start();
  76         }
  77 
  78         Thread.sleep(1);
  79 
  80         for (int i = 0; i < NTHREADS; i++) {
  81             Thread thread = threads[i];
  82 
  83             if (thread.isAlive()) {
  84                 thread.interrupt();
  85                 break;
  86             }
  87         }
  88 
  89         for (int i = 0; i < NTHREADS; i++) {
  90             Thread thread = threads[i];
  91             thread.join();
  92         }
  93     }
  94 }