Skip to content

Commit

Permalink
Merge pull request #13 from lendup/sshah/tweak_parallel_classloading
Browse files Browse the repository at this point in the history
tweak classloading logic:
  • Loading branch information
bigloser authored Jul 22, 2016
2 parents 800a918 + da2ad1a commit 5351b24
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions framework/src/play/classloading/ApplicationClassloader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package play.classloading;

import play.Logger;
import play.Play;
import play.cache.Cache;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.classloading.hash.ClassStateHashCreator;
import play.exceptions.UnexpectedException;
import play.libs.IO;
import play.vfs.VirtualFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Class;
import java.lang.annotation.Annotation;
import java.lang.instrument.ClassDefinition;
import java.net.MalformedURLException;
Expand All @@ -20,24 +28,15 @@
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import play.Logger;
import play.Play;
import play.classloading.hash.ClassStateHashCreator;
import play.vfs.VirtualFile;
import play.cache.Cache;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.exceptions.UnexpectedException;
import play.libs.IO;

/**
* The application classLoader.
* Load the classes from the application Java sources files.
Expand Down Expand Up @@ -432,35 +431,53 @@ public List<Class> getAllClasses() {

}

final int cores = Runtime.getRuntime().availableProcessors() + 1;
final ExecutorService executor = Executors.newFixedThreadPool(cores);
if (Boolean.parseBoolean(System.getenv("DISABLE_PARALLEL_CLASSLOADING"))) {
for (ApplicationClass applicationClass : Play.classes.all()) {
Class clazz = loadApplicationClass(applicationClass.name);
if (clazz != null) {
allClasses.add(clazz);
}
}
} else {
int cores = Runtime.getRuntime().availableProcessors() + 1;
String envCores = System.getenv("PARALLEL_CLASSLOADING_THREADS");
if (envCores != null) {
try {
cores = Integer.parseInt(envCores);
} catch (NumberFormatException ignored) {
}
}
final ExecutorService executor = Executors.newFixedThreadPool(cores);

Logger.info("Using %d cores.", cores);
Logger.info("Using %d cores.", cores);

for (final ApplicationClass applicationClass : Play.classes.all()) {
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() {
final Class clazz = loadApplicationClass(applicationClass.name);
List<Future<Class>> futures = new LinkedList<Future<Class>>();

if (clazz != null) {
synchronized (allClasses) {
allClasses.add(clazz);
}
for (final ApplicationClass applicationClass : Play.classes.all()) {
futures.add(executor.submit(new Callable<Class>() {
@Override
public Class call() {
return loadApplicationClass(applicationClass.name);
}
}));
}

return true;
executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
Logger.trace("Waiting for class enhancer threads to complete, sleeping 1s...");
}
});
}

executor.shutdown();
try {
while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
Logger.trace("Waiting for class enhancer threads to complete, sleeping 1s...");
for (Future<Class> future:futures) {
Class clazz = future.get();

if (clazz != null) {
allClasses.add(clazz);
}
}
} catch (Exception e) {
Logger.error(e, "Class enhancer thread failed!");
}
} catch (Exception e) {
Logger.error(e, "Class enhancer thread failed!");
}

Collections.sort(allClasses, new Comparator<Class>() {
Expand Down

0 comments on commit 5351b24

Please sign in to comment.