< prev index next >

test/java/lang/ClassLoader/deadlock/DelegateTest.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 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.MalformedURLException;
  25 import java.net.URL;




  26 
  27 public class Starter implements Runnable {



  28 
  29     private String id;
  30     private DelegatingLoader dl;
  31     private String startClass;
  32 
  33     private static DelegatingLoader saLoader, sbLoader;
  34 
  35     public static void log(String line) {
  36         System.out.println(line);
  37     }
  38 
  39     public static void main(String[] args) {
  40         URL[] urlsa = new URL[1];
  41         URL[] urlsb = new URL[1];




  42         try {
  43             String testDir = System.getProperty("test.classes", ".");
  44             String sep = System.getProperty("file.separator");
  45             urlsa[0] = new URL("file://" + testDir + sep + "SA" + sep);
  46             urlsb[0] = new URL("file://" + testDir + sep + "SB" + sep);
  47         } catch (MalformedURLException e) {
  48             e.printStackTrace();
  49         }
  50         // Set up Classloader delegation hierarchy
  51         saLoader = new DelegatingLoader(urlsa);
  52         sbLoader = new DelegatingLoader(urlsb);
  53 
  54         String[] saClasses = { "comSA.SupBob", "comSA.Alice" };
  55         String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };
  56 
  57         saLoader.setDelegate(sbClasses, sbLoader);
  58         sbLoader.setDelegate(saClasses, saLoader);
  59 
  60         // test one-way delegate
  61         String testType = args[0];
  62         if (testType.equals("one-way")) {
  63             test("comSA.Alice", "comSA.SupBob");
  64         } else if (testType.equals("cross")) {
  65             // test cross delegate
  66             test("comSA.Alice", "comSB.Bob");
  67         } else {
  68             System.out.println("ERROR: unsupported - " + testType);
  69         }
  70     }
  71 
  72     private static void test(String clsForSA, String clsForSB) {
  73         Starter ia = new Starter("SA", saLoader, clsForSA);
  74         Starter ib = new Starter("SB", sbLoader, clsForSB);
  75         new Thread(ia).start();
  76         new Thread(ib).start();




  77     }
  78 
  79     public static void sleep() {
  80         try {
  81             Thread.sleep(500);
  82         } catch (InterruptedException e) {
  83             e.printStackTrace();
  84             log("Thread interrupted");
  85         }
  86     }
  87 
  88     private Starter(String id, DelegatingLoader dl, String startClass) {
  89         this.id = id;
  90         this.dl = dl;
  91         this.startClass = startClass;
  92     }
  93 
  94     public void run() {
  95         log("Spawned thread " + id + " running");
  96         try {
  97             // To mirror the WAS deadlock, need to ensure class load
  98             // is routed via the VM.
  99             Class.forName(startClass, true, dl);
 100         } catch (ClassNotFoundException e) {
 101             e.printStackTrace();
 102         }
 103         log("Thread " + id + " terminating");
 104     }
 105 }
   1 /*
   2  * Copyright (c) 2009, 2017, 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 /*
  25  * @test
  26  * @bug 4735126
  27  * @summary (cl) ClassLoader.loadClass locks all instances in chain when delegating
  28  * @modules java.base/java.lang:open
  29  *          jdk.compiler
  30  * @library /test/lib
  31  * @build jdk.test.lib.compiler.CompilerUtils
  32  * @run main/othervm -Xlog:class+load DelegateTest one-way
  33  * @run main/othervm -Xlog:class+load DelegateTest cross
  34  */
  35 
  36 import java.io.File;
  37 import java.net.MalformedURLException;
  38 import java.net.URL;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 
  42 import jdk.test.lib.compiler.CompilerUtils;
  43 
  44 public class DelegateTest implements Runnable {
  45 
  46     private static final Path TEST_DIR = Paths.get(System.getProperty("user.dir", "."));
  47     private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), "src");
  48 
  49     private String id;
  50     private DelegatingLoader dl;
  51     private String startClass;
  52 
  53     private static DelegatingLoader saLoader, sbLoader;
  54     
  55     public static void log(String line) {
  56         System.out.println(line);
  57     }
  58 
  59     public static void main(String[] args) throws Exception {
  60         if (!CompilerUtils.compile(SRC_DIR, TEST_DIR)) {
  61             throw new RuntimeException("Failed to compile "
  62                     + SRC_DIR.toAbsolutePath().toString());
  63         }
  64         
  65         URL[] url = new URL[1];
  66         try {
  67             url[0] = new URL("file://" + TEST_DIR + File.separator);



  68         } catch (MalformedURLException e) {
  69             e.printStackTrace();
  70         }
  71         // Set up Classloader delegation hierarchy
  72         saLoader = new DelegatingLoader(url);
  73         sbLoader = new DelegatingLoader(url);
  74 
  75         String[] saClasses = { "comSA.SupBob", "comSA.Alice" };
  76         String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };
  77 
  78         saLoader.setDelegate(sbClasses, sbLoader);
  79         sbLoader.setDelegate(saClasses, saLoader);
  80 
  81         // test one-way delegate
  82         String testType = args[0];
  83         if (testType.equals("one-way")) {
  84             test("comSA.Alice", "comSA.SupBob");
  85         } else if (testType.equals("cross")) {
  86             // test cross delegate
  87             test("comSA.Alice", "comSB.Bob");
  88         } else {
  89             System.out.println("ERROR: unsupported - " + testType);
  90         }
  91     }
  92 
  93     private static void test(String clsForSA, String clsForSB) throws InterruptedException {
  94         DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA);
  95         DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB);
  96         Thread ta = new Thread(ia);
  97         Thread tb = new Thread(ib);
  98         ta.start();
  99         tb.start();
 100         ta.join();
 101         tb.join();
 102     }
 103 
 104     public static void sleep() {
 105         try {
 106             Thread.sleep(500);
 107         } catch (InterruptedException e) {
 108             e.printStackTrace();
 109             log("Thread interrupted");
 110         }
 111     }
 112 
 113     private DelegateTest(String id, DelegatingLoader dl, String startClass) {
 114         this.id = id;
 115         this.dl = dl;
 116         this.startClass = startClass;
 117     }
 118 
 119     public void run() {
 120         log("Spawned thread " + id + " running");
 121         try {
 122             // To mirror the WAS deadlock, need to ensure class load
 123             // is routed via the VM.
 124             Class.forName(startClass, true, dl);
 125         } catch (ClassNotFoundException e) {
 126             e.printStackTrace();
 127         }
 128         log("Thread " + id + " terminating");
 129     }
 130 }
< prev index next >