Skip to content

Commit f13f3b0

Browse files
author
Scott Oaks
committed
Initial push.
1 parent 815141c commit f13f3b0

File tree

168 files changed

+22470
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+22470
-4
lines changed

ClassLoading/README

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
To build:
2+
ant
3+
4+
To run:
5+
java <jvmargs> -jar jars/classloading.jar jarfile nThreads useLargeStack
6+
7+
jarfile -- All classes from this jar file will be loaded in the customer
8+
class loader
9+
nThreads -- Number of threads to execute simultaneously
10+
useLargeStack -- If true, then the lookup will be performed from a
11+
call stack of 40 entries
12+
13+
In general, the examples leverage the modules from glassfish, but any
14+
large set of jar files and classes can be used.
15+
16+
Notes on Examples:
17+
18+
Example 12-2
19+
java -jar jars/classloading.jar /path_to/guava.jar N false
20+
where N varies from 1 to 8
21+
22+
Example 12-3
23+
java -jar jars/classloading.jar /path_to/guava.jar N false
24+
where N varies from 1 to 8
25+
and where the CLASSPATH contains all jar files from glassfish
26+
(266 entries)

ClassLoading/build.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0"?>
2+
3+
<project name="ClassLoading" default="jar">
4+
5+
<target name="init">
6+
<mkdir dir="classes"/>
7+
<mkdir dir="jars"/>
8+
</target>
9+
10+
<target name="compile" depends="init">
11+
<property environment="env"/>
12+
<javac includeantruntime="false" srcdir="src"
13+
debug="true" destdir="classes">
14+
</javac>
15+
</target>
16+
17+
<target name="jar" depends="compile">
18+
<jar jarfile="jars/classloading.jar">
19+
<manifest>
20+
<attribute name="Main-Class" value="net.sdo.ClassLoaderTest"/>
21+
</manifest>
22+
<fileset dir="classes" includes="**/*.class"/>
23+
</jar>
24+
</target>
25+
26+
<target name="clean">
27+
<delete>
28+
<fileset dir="classes"/>
29+
<fileset dir="jars"/>
30+
</delete>
31+
</target>
32+
</project>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
3+
*/
4+
5+
package net.sdo;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.MalformedURLException;
10+
import java.net.URL;
11+
import java.net.URLClassLoader;
12+
import java.util.ArrayList;
13+
import java.util.Enumeration;
14+
import java.util.concurrent.LinkedBlockingQueue;
15+
import java.util.concurrent.atomic.AtomicLong;
16+
import java.util.zip.ZipEntry;
17+
import java.util.zip.ZipException;
18+
import java.util.zip.ZipFile;
19+
20+
public class ClassLoaderTest implements Runnable {
21+
22+
private static String[] classNames;
23+
private static AtomicLong time = new AtomicLong();
24+
private static URL[] urls;
25+
private static URLClassLoader ucl;
26+
27+
private static boolean increaseStackDepth = false;
28+
29+
private static void loadClasses(String[] classNames, int count, ClassLoader cl) throws ClassNotFoundException {
30+
for (int i = 0; i < count; i++) {
31+
String name = classNames[i];
32+
cl.loadClass(name);
33+
}
34+
}
35+
36+
private static void initializeFromJar(String fileName) throws ZipException, IOException {
37+
ZipFile zf = new ZipFile(new File(fileName));
38+
Enumeration e = zf.entries();
39+
ArrayList<String> al = new ArrayList<String>();
40+
while (e.hasMoreElements()) {
41+
ZipEntry ze = (ZipEntry) e.nextElement();
42+
String s = ze.getName();
43+
if (s.endsWith(".class")) {
44+
al.add(s);
45+
}
46+
}
47+
String[] names = al.toArray(new String[al.size()]);
48+
classNames = new String[names.length];
49+
for (int i = 0; i < names.length; i++) {
50+
classNames[i] = names[i].substring(0, names[i].length() - 6).replaceAll("/", ".");
51+
}
52+
}
53+
54+
public void a1() { a2(); }
55+
public void a2() { a3(); }
56+
public void a3() { a4(); }
57+
public void a4() { a5(); }
58+
public void a5() { a6(); }
59+
public void a6() { a7(); }
60+
public void a7() { a8(); }
61+
public void a8() { a9(); }
62+
public void a9() { a10(); }
63+
public void a10() { a11(); }
64+
public void a11() { a12(); }
65+
public void a12() { a13(); }
66+
public void a13() { a14(); }
67+
public void a14() { a15(); }
68+
public void a15() { a16(); }
69+
public void a16() { a17(); }
70+
public void a17() { a18(); }
71+
public void a18() { a19(); }
72+
public void a19() { a20(); }
73+
public void a20() { a21(); }
74+
public void a21() { a22(); }
75+
public void a22() { a23(); }
76+
public void a23() { a24(); }
77+
public void a24() { a25(); }
78+
public void a25() { a26(); }
79+
public void a26() { a27(); }
80+
public void a27() { a28(); }
81+
public void a28() { a29(); }
82+
public void a29() { a30(); }
83+
public void a30() { a31(); }
84+
public void a31() { a32(); }
85+
public void a32() { a33(); }
86+
public void a33() { a34(); }
87+
public void a34() { a35(); }
88+
public void a35() { a36(); }
89+
public void a36() { a37(); }
90+
public void a37() { a38(); }
91+
public void a38() { a39(); }
92+
public void a39() { a40(); }
93+
public void a40() {
94+
try {
95+
loadClasses(classNames, classNames.length, new URLClassLoader(urls));
96+
} catch (ClassNotFoundException ex) {
97+
System.err.println("Can't define class ");
98+
ex.printStackTrace();
99+
System.exit(-1);
100+
}
101+
}
102+
103+
public void run() {
104+
try {
105+
if (increaseStackDepth) {
106+
a1();
107+
}
108+
else {
109+
loadClasses(classNames, classNames.length, new URLClassLoader(urls));
110+
}
111+
} catch (Exception e) {
112+
System.err.println("Can't define class ");
113+
e.printStackTrace();
114+
System.exit(-1);
115+
}
116+
}
117+
118+
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, ZipException, IOException {
119+
initializeFromJar(args[0]);
120+
int nThreads = Integer.parseInt(args[1]);
121+
increaseStackDepth = Boolean.parseBoolean(args[2]);
122+
urls = new URL[]{new File(args[0]).toURL()};
123+
for (int i = 0; i < 10; i++) {
124+
ucl = new URLClassLoader(urls);
125+
loadClasses(classNames, classNames.length, ucl);
126+
}
127+
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
128+
PausingThreadPoolExecutor tpe = new PausingThreadPoolExecutor(nThreads, queue);
129+
tpe.prestartAllCoreThreads();
130+
for (int j = 0; j < 100; j++) {
131+
tpe.pause();
132+
ucl = new URLClassLoader(urls);
133+
for (int i = 0; i < nThreads; i++) {
134+
tpe.addTask(new ClassLoaderTest());
135+
}
136+
long then = System.currentTimeMillis();
137+
tpe.resume();
138+
long now = System.currentTimeMillis();
139+
time.getAndAdd(now - then);
140+
}
141+
tpe.shutdown();
142+
System.out.println("Threads: " + nThreads + ", Classes: " + classNames.length + ", Time: " + time);
143+
}
144+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
3+
*/
4+
5+
package net.sdo;
6+
7+
import java.util.concurrent.BlockingQueue;
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.ThreadPoolExecutor;
10+
import java.util.concurrent.TimeUnit;
11+
import java.util.concurrent.locks.Condition;
12+
import java.util.concurrent.locks.ReentrantLock;
13+
14+
public class PausingThreadPoolExecutor extends ThreadPoolExecutor {
15+
16+
private boolean isPaused;
17+
private ReentrantLock pauseLock = new ReentrantLock();
18+
private Condition unpaused = pauseLock.newCondition();
19+
private CountDownLatch latch;
20+
private BlockingQueue<Runnable> queue;
21+
private int latchCount;
22+
23+
public PausingThreadPoolExecutor(int nThreads, BlockingQueue<Runnable> queue) {
24+
super(nThreads, nThreads, Long.MAX_VALUE, TimeUnit.DAYS, queue);
25+
this.queue = queue;
26+
}
27+
28+
@Override
29+
protected void beforeExecute(Thread t, Runnable r) {
30+
super.beforeExecute(t, r);
31+
pauseLock.lock();
32+
try {
33+
while (isPaused) {
34+
unpaused.await();
35+
}
36+
} catch (InterruptedException ie) {
37+
t.interrupt();
38+
} finally {
39+
pauseLock.unlock();
40+
}
41+
}
42+
43+
public void addTask(Runnable r) {
44+
latchCount++;
45+
queue.add(r);
46+
}
47+
48+
@Override
49+
protected void afterExecute(Runnable r, Throwable t) {
50+
latch.countDown();
51+
}
52+
53+
public void pause() {
54+
pauseLock.lock();
55+
try {
56+
isPaused = true;
57+
} finally {
58+
pauseLock.unlock();
59+
}
60+
}
61+
62+
public void resume() {
63+
pauseLock.lock();
64+
latch = new CountDownLatch(latchCount);
65+
latchCount = 0;
66+
try {
67+
isPaused = false;
68+
unpaused.signalAll();
69+
} finally {
70+
pauseLock.unlock();
71+
}
72+
try {
73+
latch.await();
74+
} catch (InterruptedException ex) {
75+
}
76+
}
77+
}

CountFilter/build.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0"?>
2+
3+
<project name="CountFilter" default="jar">
4+
5+
<target name="init">
6+
<mkdir dir="classes"/>
7+
<mkdir dir="jars"/>
8+
</target>
9+
10+
<target name="compile" depends="init">
11+
<property environment="env"/>
12+
<javac includeantruntime="false" srcdir="src"
13+
debug="true" destdir="classes">
14+
</javac>
15+
</target>
16+
17+
<target name="jar" depends="compile">
18+
<jar jarfile="jars/countfilter.jar">
19+
<manifest>
20+
<attribute name="Main-Class" value="net.sdo.CountFilter"/>
21+
</manifest>
22+
<fileset dir="classes" includes="**/*.class"/>
23+
</jar>
24+
</target>
25+
26+
<target name="clean">
27+
<delete>
28+
<fileset dir="classes"/>
29+
<fileset dir="jars"/>
30+
</delete>
31+
</target>
32+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
3+
*/
4+
5+
package net.sdo;
6+
7+
import java.util.ArrayList;
8+
import java.util.Optional;
9+
import java.util.stream.Stream;
10+
11+
public class CountFilter {
12+
private static boolean verbose;
13+
private static volatile int count = 0;
14+
15+
private static long countFilter(ArrayList<String> al) {
16+
long then = System.currentTimeMillis();
17+
Stream<String> stream = al.stream();
18+
stream.filter(
19+
symbol -> symbol.charAt(0) != 'A' &&
20+
symbol.charAt(1) != 'A' &&
21+
symbol.charAt(2) != 'A' &&
22+
symbol.charAt(3) != 'A').
23+
forEach(symbol -> { count++; });
24+
long now = System.currentTimeMillis();
25+
return now - then;
26+
}
27+
28+
private static long countIterator(ArrayList<String> al) {
29+
long then = System.currentTimeMillis();
30+
for (String symbol : al) {
31+
if (symbol.charAt(0) != 'A' &&
32+
symbol.charAt(1) != 'A' &&
33+
symbol.charAt(2) != 'A' &&
34+
symbol.charAt(3) != 'A')
35+
count++;
36+
}
37+
return System.currentTimeMillis() - then;
38+
}
39+
40+
public static void main(String[] args) {
41+
verbose = Boolean.getBoolean("verbose");
42+
ArrayList<String> al = new ArrayList<>(500000);
43+
for (int first = (int) 'A'; first <= (int) 'Z'; first++) {
44+
for (int second = 'A'; second <= 'Z'; second++) {
45+
for (int third = 'A'; third <= 'Z'; third++) {
46+
for (int fourth = 'A'; fourth <= 'Z'; fourth++) {
47+
char[] c = new char[4];
48+
c[0] = (char) first;
49+
c[1] = (char) second;
50+
c[2] = (char) third;
51+
c[3] = (char) fourth;
52+
al.add(new String(c));
53+
}
54+
}
55+
}
56+
}
57+
System.out.println("Start");
58+
long time = 0;
59+
for (int i = 0; i < 1000; i++) {
60+
switch (args[0]) {
61+
case "Filter":
62+
time += countFilter(al);
63+
break;
64+
case "Iterator":
65+
time += countIterator(al);
66+
break;
67+
}
68+
}
69+
System.out.println("Operation took " + time + " ms for " + count);
70+
}
71+
}

Exception/README

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
To build:
2+
ant
3+
4+
To run:
5+
java <jvmargs> -jar jars/exception.jar numLoops pctExceptions
6+
7+
Example 12-5:
8+
Run with 1000000 100
9+
10+
Example 12-6:
11+
Run with 1000000 1

0 commit comments

Comments
 (0)