jdk/test/java/lang/ClassLoader/deadlock/Starter.java
changeset 45957 d3eb7790a38d
parent 45956 832f85797be8
parent 45954 58c92a58e1ef
child 45959 554ed368461d
equal deleted inserted replaced
45956:832f85797be8 45957:d3eb7790a38d
     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 }