1 /*
   2  * Copyright (c) 2015, 2019, 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  * @test
  35  * @summary Test to see if thread interrupt handling interferes with other threads.
  36  * @run main/othervm -Djdk.image.map.all=false JImageOpenTest
  37  */
  38 public class JImageOpenTest {
  39     private static final int NTHREADS = 10;
  40 
  41     public static void main(String[] args) throws Exception {
  42 
  43         final FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
  44         final Path root = fs.getPath("/modules");
  45 
  46         final List<String> names = Files.walk(root)
  47                 .filter(p -> p.getNameCount() > 2)
  48                 .filter(p -> ModuleLayer.boot().findModule(p.getName(1).toString()).isPresent())
  49                 .map(p -> p.subpath(2, p.getNameCount()))
  50                 .map(p -> p.toString())
  51                 .filter(s -> s.endsWith(".class") && !s.endsWith("module-info.class"))
  52                 .collect(Collectors.toList());
  53 
  54         Runnable r = new Runnable() {
  55             @Override
  56             public void run() {
  57                 names.forEach(name -> {
  58                     String cn = name.substring(0, name.length() - 6).replace('/', '.');
  59                     try {
  60                         Class.forName(cn, false, ClassLoader.getSystemClassLoader());
  61                     } catch (Exception ex) {
  62                         System.err.println(Thread.currentThread() + " " + ex.getClass());
  63                     }
  64                 });
  65             }
  66         };
  67 
  68         Thread[] threads = new Thread[NTHREADS];
  69 
  70         for (int i = 0; i < NTHREADS; i++) {
  71             Thread thread =  new Thread(r);
  72             threads[i] = thread;
  73             thread.start();
  74         }
  75 
  76         Thread.sleep(1);
  77 
  78         for (int i = 0; i < NTHREADS; i++) {
  79             Thread thread = threads[i];
  80 
  81             if (thread.isAlive()) {
  82                 thread.interrupt();
  83                 break;
  84             }
  85         }
  86 
  87         for (int i = 0; i < NTHREADS; i++) {
  88             Thread thread = threads[i];
  89             thread.join();
  90         }
  91     }
  92 }