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 import java.util.ArrayList;
  25 import java.util.Enumeration;
  26 import java.util.List;
  27 import java.util.jar.JarEntry;
  28 import java.util.jar.JarFile;
  29 import java.util.regex.Matcher;
  30 import java.util.regex.Pattern;
  31 
  32 abstract class Task<T> implements Runnable {
  33     private transient boolean working = true;
  34     private final List<T> methods;
  35     private final Thread thread;
  36 
  37     Task(List<T> methods) {
  38         this.methods = methods;
  39         this.thread = new Thread(this);
  40         this.thread.start();
  41     }
  42 
  43     boolean isAlive() {
  44         return this.thread.isAlive();
  45     }
  46 
  47     boolean isWorking() {
  48         boolean working = this.working && this.thread.isAlive();
  49         this.working = false;
  50         return working;
  51     }
  52 
  53     @Override
  54     public void run() {
  55         long time = -System.currentTimeMillis();
  56         for (T method : this.methods) {
  57             this.working = true;
  58             try {
  59                 for (int i = 0; i < 100; i++) {
  60                     process(method);
  61                 }
  62             } catch (NoSuchMethodException ignore) {
  63             }
  64         }
  65         time += System.currentTimeMillis();
  66         print("thread done in " + time / 1000 + " seconds");
  67     }
  68 
  69     protected abstract void process(T method) throws NoSuchMethodException;
  70 
  71     static synchronized void print(Object message) {
  72         System.out.println(message);
  73         System.out.flush();
  74     }
  75 
  76     static List<Class<?>> getClasses(int count) throws Exception {
  77         String resource = ClassLoader.getSystemClassLoader().getResource("java/lang/Object.class").toString();
  78 
  79         Pattern pattern = Pattern.compile("jar:file:(.*)!.*");
  80         Matcher matcher = pattern.matcher(resource);
  81         matcher.matches();
  82         resource = matcher.group(1);
  83 
  84         List<Class<?>> classes = new ArrayList<>();
  85         try (JarFile jarFile = new JarFile(resource)) {
  86             Enumeration<JarEntry> entries = jarFile.entries();
  87             while (entries.hasMoreElements()) {
  88                 String name = entries.nextElement().getName();
  89                 if (name.startsWith("java") && name.endsWith(".class")) {
  90                     classes.add(Class.forName(name.substring(0, name.indexOf(".")).replace('/', '.')));
  91                     if (count == classes.size()) {
  92                         break;
  93                     }
  94                 }
  95             }
  96         }
  97         return classes;
  98     }
  99 }