1 /*
2 * Copyright (c) 2013, 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 8022595
27 * @summary JSR292: deadlock during class loading of MethodHandles, MethodHandleImpl & MethodHandleNatives
28 *
29 * @run main/othervm ConcurrentClassLoadingTest
30 */
31 import java.util.*;
32 import java.util.concurrent.BrokenBarrierException;
33 import java.util.concurrent.CyclicBarrier;
34
35 public class ConcurrentClassLoadingTest {
36 int numThreads = 0;
37 long seed = 0;
38 CyclicBarrier l;
39 Random rand;
40
41 public static void main(String[] args) throws Throwable {
42 ConcurrentClassLoadingTest test = new ConcurrentClassLoadingTest();
43 test.parseArgs(args);
44 test.run();
45 }
46
47 void parseArgs(String[] args) {
48 int i = 0;
49 while (i < args.length) {
50 String flag = args[i];
51 switch(flag) {
52 case "-seed":
53 seed = Long.parseLong(args[++i]);
54 break;
55 case "-numThreads":
56 numThreads = Integer.parseInt(args[++i]);
57 break;
58 default:
59 throw new Error("Unknown flag: " + flag);
60 }
61 ++i;
62 }
63 }
64
65 void init() {
66 if (numThreads == 0) {
67 numThreads = Runtime.getRuntime().availableProcessors();
68 }
69
70 if (seed == 0) {
71 seed = (new Random()).nextLong();
72 }
73 rand = new Random(seed);
74
75 l = new CyclicBarrier(numThreads + 1);
76
77 System.out.printf("Threads: %d\n", numThreads);
78 System.out.printf("Seed: %d\n", seed);
79 }
80
81 final List<Loader> loaders = new ArrayList<>();
82
83 void prepare() {
84 List<String> c = new ArrayList<>(Arrays.asList(classNames));
85
86 // Split classes between loading threads
87 int count = (classNames.length / numThreads) + 1;
88 for (int t = 0; t < numThreads; t++) {
89 List<String> sel = new ArrayList<>();
90
91 System.out.printf("Thread #%d:\n", t);
92 for (int i = 0; i < count; i++) {
93 if (c.size() == 0) break;
94
95 int k = rand.nextInt(c.size());
96 String elem = c.remove(k);
97 sel.add(elem);
98 System.out.printf("\t%s\n", elem);
99 }
100 loaders.add(new Loader(sel));
101 }
102
103 // Print diagnostic info when the test hangs
104 Runtime.getRuntime().addShutdownHook(new Thread() {
105 public void run() {
106 boolean alive = false;
107 for (Loader l : loaders) {
108 if (!l.isAlive()) continue;
109
110 if (!alive) {
111 System.out.println("Some threads are still alive:");
112 alive = true;
113 }
|
1 /*
2 * Copyright (c) 2013, 2014, 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 8022595
27 * @summary JSR292: deadlock during class loading of MethodHandles, MethodHandleImpl & MethodHandleNatives
28 * @library /testlibrary
29 * @run main/othervm ConcurrentClassLoadingTest
30 */
31 import com.oracle.java.testlibrary.Utils;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Random;
36 import java.util.concurrent.BrokenBarrierException;
37 import java.util.concurrent.CyclicBarrier;
38
39 public class ConcurrentClassLoadingTest {
40 int numThreads = 0;
41 CyclicBarrier l;
42 private static final Random rand = Utils.getRandomInstance();
43
44 public static void main(String[] args) throws Throwable {
45 ConcurrentClassLoadingTest test = new ConcurrentClassLoadingTest();
46 test.parseArgs(args);
47 test.run();
48 }
49
50 void parseArgs(String[] args) {
51 int i = 0;
52 while (i < args.length) {
53 String flag = args[i];
54 switch(flag) {
55 case "-numThreads":
56 numThreads = Integer.parseInt(args[++i]);
57 break;
58 default:
59 throw new Error("Unknown flag: " + flag);
60 }
61 ++i;
62 }
63 }
64
65 void init() {
66 if (numThreads == 0) {
67 numThreads = Runtime.getRuntime().availableProcessors();
68 }
69
70 l = new CyclicBarrier(numThreads + 1);
71
72 System.out.printf("Threads: %d\n", numThreads);
73 }
74
75 final List<Loader> loaders = new ArrayList<>();
76
77 void prepare() {
78 List<String> c = new ArrayList<>(Arrays.asList(classNames));
79
80 // Split classes between loading threads
81 int count = (classNames.length / numThreads) + 1;
82 for (int t = 0; t < numThreads; t++) {
83 List<String> sel = new ArrayList<>();
84
85 System.out.printf("Thread #%d:\n", t);
86 for (int i = 0; i < count; i++) {
87 if (c.isEmpty()) {
88 break;
89 }
90
91 int k = rand.nextInt(c.size());
92 String elem = c.remove(k);
93 sel.add(elem);
94 System.out.printf("\t%s\n", elem);
95 }
96 loaders.add(new Loader(sel));
97 }
98
99 // Print diagnostic info when the test hangs
100 Runtime.getRuntime().addShutdownHook(new Thread() {
101 public void run() {
102 boolean alive = false;
103 for (Loader l : loaders) {
104 if (!l.isAlive()) continue;
105
106 if (!alive) {
107 System.out.println("Some threads are still alive:");
108 alive = true;
109 }
|