< 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  * @build Alice Bob SupAlice SupBob
  30  * @run driver SetupLoader
  31  * @run main/othervm -Xlog:class+load DelegateTest one-way
  32  * @run main/othervm -Xlog:class+load DelegateTest cross
  33  */
  34 
  35 import java.net.MalformedURLException;
  36 import java.net.URL;
  37 
  38 public class DelegateTest implements Runnable {
  39 
  40     private String id;
  41     private DelegatingLoader dl;
  42     private String startClass;
  43 
  44     private static DelegatingLoader saLoader, sbLoader;
  45 
  46     public static void log(String line) {
  47         System.out.println(line);
  48     }
  49 
  50     public static void main(String[] args) throws Exception {
  51         URL[] urlsa = new URL[1];
  52         URL[] urlsb = new URL[1];
  53         try {
  54             String testDir = System.getProperty("user.dir", ".");
  55             String sep = System.getProperty("file.separator");
  56             urlsa[0] = new URL("file://" + testDir + sep + "SA" + sep);
  57             urlsb[0] = new URL("file://" + testDir + sep + "SB" + sep);
  58         } catch (MalformedURLException e) {
  59             e.printStackTrace();
  60         }
  61         // Set up Classloader delegation hierarchy
  62         saLoader = new DelegatingLoader(urlsa);
  63         sbLoader = new DelegatingLoader(urlsb);
  64 
  65         String[] saClasses = { "comSA.SupBob", "comSA.Alice" };
  66         String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };
  67 
  68         saLoader.setDelegate(sbClasses, sbLoader);
  69         sbLoader.setDelegate(saClasses, saLoader);
  70 
  71         // test one-way delegate
  72         String testType = args[0];
  73         if (testType.equals("one-way")) {
  74             test("comSA.Alice", "comSA.SupBob");
  75         } else if (testType.equals("cross")) {
  76             // test cross delegate
  77             test("comSA.Alice", "comSB.Bob");
  78         } else {
  79             System.out.println("ERROR: unsupported - " + testType);
  80         }
  81     }
  82 
  83     private static void test(String clsForSA, String clsForSB) throws InterruptedException {
  84         DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA);
  85         DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB);
  86         Thread ta = new Thread(ia);
  87         Thread tb = new Thread(ib);
  88         ta.start();
  89         tb.start();
  90         ta.join();
  91         tb.join();
  92     }
  93 
  94     public static void sleep() {
  95         try {
  96             Thread.sleep(500);
  97         } catch (InterruptedException e) {
  98             e.printStackTrace();
  99             log("Thread interrupted");
 100         }
 101     }
 102 
 103     private DelegateTest(String id, DelegatingLoader dl, String startClass) {
 104         this.id = id;
 105         this.dl = dl;
 106         this.startClass = startClass;
 107     }
 108 
 109     public void run() {
 110         log("Spawned thread " + id + " running");
 111         try {
 112             // To mirror the WAS deadlock, need to ensure class load
 113             // is routed via the VM.
 114             Class.forName(startClass, true, dl);
 115         } catch (ClassNotFoundException e) {
 116             e.printStackTrace();
 117         }
 118         log("Thread " + id + " terminating");
 119     }
 120 }
< prev index next >