Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLDecoder/8028054/Task.java
38828 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.Enumeration;25import java.util.List;26import java.util.jar.JarEntry;27import java.util.jar.JarFile;28import java.util.regex.Matcher;29import java.util.regex.Pattern;3031abstract class Task<T> implements Runnable {32private transient boolean working = true;33private final List<T> methods;34private final Thread thread;3536Task(List<T> methods) {37this.methods = methods;38this.thread = new Thread(this);39this.thread.start();40}4142boolean isAlive() {43return this.thread.isAlive();44}4546boolean isWorking() {47boolean working = this.working && this.thread.isAlive();48this.working = false;49return working;50}5152@Override53public void run() {54long time = -System.currentTimeMillis();55for (T method : this.methods) {56this.working = true;57try {58for (int i = 0; i < 100; i++) {59process(method);60}61} catch (NoSuchMethodException ignore) {62}63}64time += System.currentTimeMillis();65print("thread done in " + time / 1000 + " seconds");66}6768protected abstract void process(T method) throws NoSuchMethodException;6970static synchronized void print(Object message) {71System.out.println(message);72System.out.flush();73}7475static List<Class<?>> getClasses(int count) throws Exception {76String resource = ClassLoader.getSystemClassLoader().getResource("java/lang/Object.class").toString();7778Pattern pattern = Pattern.compile("jar:file:(.*)!.*");79Matcher matcher = pattern.matcher(resource);80matcher.matches();81resource = matcher.group(1);8283List<Class<?>> classes = new ArrayList<>();84try (JarFile jarFile = new JarFile(resource)) {85Enumeration<JarEntry> entries = jarFile.entries();86while (entries.hasMoreElements()) {87String name = entries.nextElement().getName();88if (name.startsWith("java") && name.endsWith(".class")) {89classes.add(Class.forName(name.substring(0, name.indexOf(".")).replace('/', '.')));90if (count == classes.size()) {91break;92}93}94}95}96return classes;97}98}99100101