Skip to content

Instantly share code, notes, and snippets.

@juliangamble
Created July 16, 2011 04:56
Show Gist options
  • Save juliangamble/1086017 to your computer and use it in GitHub Desktop.
Save juliangamble/1086017 to your computer and use it in GitHub Desktop.
Test of Thread and Stack limits in Java - 1000 of each
package complexGC;
import java.util.ArrayList;
import java.util.List;
public class ComplexGCThreadedDemo {
private static final int TEN_SECONDS = 10000;
private static final int NUM_THREADS = 1000;
private static final boolean ENABLE_GC_BASIC = false;
private static final boolean ENABLE_GC_VARIGATED = true;
/**
* @param args
*/
public static void main(String[] args) {
ComplexGCThreadedDemo demo = new ComplexGCThreadedDemo();
demo.runTest();
}
private void runTest() {
// 1. Spin up 1000 threads
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < NUM_THREADS; i++) {
Runnable runner = new MyThread(i);
Thread thread = new Thread(runner);
thread.start();
threads.add(thread);
}
// 2. Wait for 10 seconds
sleep();
// 3. Allocate their parent reference to null
if (ENABLE_GC_BASIC)
threads = null;
if (ENABLE_GC_VARIGATED) {
for (int i = 0; i < NUM_THREADS; i++) {
// null reference every second one
threads.set(i, null);
i++;
}
}
//4. request and encourage GC
for (int i = 0; i < 10; i++) {
System.out.println("encouraging gc:" + i);
System.gc();
}
}
private void sleep() {
try {
Thread.sleep(TEN_SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyThread implements Runnable {
private static final long ONE_SECOND = 1000;
private static final long SIXTY_SECONDS = 60000;
private static final int TREE_ELEMENT_SIZE = 1000;
private static final long FIVE_SECONDS = 5000;
private int id;
public MyThread(int id) {
this.id = id;
System.out.println("Thread id: " + id + " constructed.");
populateObjectTree();
}
private void populateObjectTree() {
List<Integer> numbers = getNumbers();
Node rootNode = new Node(TREE_ELEMENT_SIZE /2, id, 0);
insertNumbers(rootNode, numbers);
}
private void insertNumbers(Node rootNode, List<Integer> numbers) {
for (Integer num : numbers) {
insert(rootNode, num);
}
}
private void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Thread id " + id + " inserted " + value + " to left of node "
+ node.value + " at depth " + node.depth + 1);
node.left = new Node(value, id, node.depth + 1);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Thread id " + id + " inserted " + value + " to right of node "
+ node.value + " at depth " + node.depth + 1);
node.right = new Node(value, id, node.depth + 1);
}
} else if (value == node.value) {
System.out.println("They match! Not inserting that one! " + value);
}
}
private List<Integer> getNumbers() {
List<Integer> numList = new ArrayList<Integer>();
for (int i = 0; i < TREE_ELEMENT_SIZE; i++) {
numList.add(i);
}
java.util.Collections.shuffle(numList);
// System.out.println(numList);
return numList;
}
@Override
public void run() {
System.out.println("Thread id: " + id + " running.");
try {
//Thread.sleep(ONE_SECOND);
Thread.sleep(FIVE_SECONDS);
//Thread.sleep(SIXTY_SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void finalize() {
System.out.println("Thread id: " + id + " finalized.");
}
}
class Node {
Node left;
Node right;
int value;
int depth;
int parentId;
public Node(int value, int parentId, int depth) {
this.value = value;
this.parentId = parentId;
this.depth = depth;
}
protected void finalize() {
System.out.println("Parent Thread id: " + parentId + " value " + value + " at depth " + depth + " finalized.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment