jdk/test/java/security/Security/ClassLoaderDeadlock/Deadlock2.java
author jjg
Mon, 25 Nov 2013 17:42:28 -0800
changeset 21894 3535c1819067
parent 5506 202f599c92aa
permissions -rw-r--r--
8028318: [doclint] doclint will reject existing user-written doc comments using custom tags that follow the recommended rules Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2590
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     4
 *
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     8
 *
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    13
 * accompanied this code).
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    14
 *
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2590
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2590
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2590
diff changeset
    21
 * questions.
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    22
 */
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    23
import java.io.*;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    24
import javax.xml.parsers.DocumentBuilderFactory;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    25
import java.security.*;
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    26
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    27
public class Deadlock2 {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    28
    public static void main(String[] args) throws Exception {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    29
        File file = new File("object.tmp");
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    30
        final byte[] bytes = new byte[(int) file.length()];
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    31
        FileInputStream fileInputStream = new FileInputStream(file);
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    32
        int read = fileInputStream.read(bytes);
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    33
        if (read != file.length()) {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    34
            throw new Exception("Didn't read all");
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    35
        }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    36
        Thread.sleep(1000);
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    37
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    38
        Runnable xmlRunnable = new Runnable() {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    39
                public void run() {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    40
                    try {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    41
                        DocumentBuilderFactory.newInstance();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    42
                    } catch (Exception e) {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    43
                        e.printStackTrace();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    44
                    }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    45
                }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    46
            };
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    47
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    48
        Runnable readObjectRunnable = new Runnable() {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    49
                public void run() {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    50
                    try {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    51
                        ObjectInputStream objectInputStream =
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    52
                            new ObjectInputStream(new ByteArrayInputStream(bytes));
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    53
                        Object o = objectInputStream.readObject();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    54
                        System.out.println(o.getClass());
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    55
                    } catch (Exception e) {
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    56
                        e.printStackTrace();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    57
                    }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    58
                }
2590
52b974d64f18 6829098: Regression test java/security/Security/ClassLoaderDeadlock/Deadlock2.java error - missing ";"
valeriep
parents: 2449
diff changeset
    59
            };
2449
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    60
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    61
        Thread thread1 = new Thread(readObjectRunnable, "Read Object");
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    62
        Thread thread2 = new Thread(xmlRunnable, "XML");
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    63
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    64
        thread1.start();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    65
        thread2.start();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    66
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    67
        thread1.join();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    68
        thread2.join();
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    69
    }
509945aa3d2a 6440846: (cl) Deadlock between AppClassLoader and ExtClassLoader
valeriep
parents:
diff changeset
    70
}