1 /*
   2  * Copyright (c) 2013, 2016, 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 com.sun.beans.finder.MethodFinder;
  25 
  26 import java.lang.reflect.Method;
  27 import java.util.ArrayList;
  28 import java.util.Collections;
  29 import java.util.List;
  30 
  31 /*
  32  * @test
  33  * @bug 8028054
  34  * @summary Tests that cached methods have synchronized access
  35  * @author Sergey Malenkov
  36  * @modules java.desktop/com.sun.beans.finder
  37  *          java.activation
  38  *          java.transaction
  39  *          java.corba
  40  *          java.xml.bind
  41  * @compile -XDignore.symbol.file TestMethodFinder.java
  42  * @run main/othervm --add-modules=java.activation,java.transaction,java.corba,java.xml.bind TestMethodFinder
  43  */
  44 
  45 public class TestMethodFinder {
  46     public static void main(String[] args) throws Exception {
  47         List<Class<?>> classes = Task.getClasses(4000);
  48         List<Method> methods = new ArrayList<>();
  49         for (Class<?> type : classes) {
  50             Collections.addAll(methods, type.getMethods());
  51         }
  52         Task.print("found " + methods.size() + " methods in " + classes.size() + " classes");
  53 
  54         List<Task> tasks = new ArrayList<>();
  55         for (int i = 0; i < 50; i++) {
  56             tasks.add(new Task<Method>(methods) {
  57                 @Override
  58                 protected void process(Method method) throws NoSuchMethodException {
  59                     MethodFinder.findMethod(method.getDeclaringClass(), method.getName(), method.getParameterTypes());
  60                 }
  61             });
  62         }
  63         int alarm = 0;
  64         while (true) {
  65             int alive = 0;
  66             int working = 0;
  67             for (Task task : tasks) {
  68                 if (task.isWorking()) {
  69                     working++;
  70                     alive++;
  71                 } else if (task.isAlive()) {
  72                     alive++;
  73                 }
  74             }
  75             if (alive == 0) {
  76                 break;
  77             }
  78             Task.print(working + " out of " + alive + " threads are working");
  79             if ((working == 0) && (++alarm == 10)) {
  80                 throw new RuntimeException("FAIL - DEADLOCK DETECTED");
  81             }
  82             Thread.sleep(1000);
  83         }
  84     }
  85 }