Skip to content

Instantly share code, notes, and snippets.

@programus
Created March 5, 2012 14:18
Show Gist options
  • Save programus/1978494 to your computer and use it in GitHub Desktop.
Save programus/1978494 to your computer and use it in GitHub Desktop.
Thread Safe Demos
import java.util.Random;
public class StringBufferVsStringBuilder {
public static int demo(final Object stringJoiner, final int testCount) throws InterruptedException {
ThreadGroup group = new ThreadGroup(stringJoiner.getClass().getName() + "@" + stringJoiner.hashCode());
final Random rand = new Random();
Runnable listAppender = new Runnable() {
public void run() {
try {
Thread.sleep(rand.nextInt(2));
} catch (InterruptedException e) {
return;
}
if (stringJoiner instanceof StringBuffer) {
((StringBuffer)stringJoiner).append("0");
} else if (stringJoiner instanceof StringBuilder) {
((StringBuilder)stringJoiner).append("0");
}
}
};
for (int i = 0; i < testCount; i++) {
new Thread(group, listAppender, "InsertList-" + i).start();
}
while (group.activeCount() > 0) {
Thread.sleep(10);
}
return stringJoiner.toString().length();
}
public static void main(String[] args) throws InterruptedException {
StringBuilder stringBuilder = new StringBuilder();
StringBuffer stringBuffer = new StringBuffer();
final int N = 10000;
for (int i = 0; i < 10; i++) {
stringBuilder.delete(0, stringBuilder.length());
stringBuffer.delete(0, stringBuffer.length());
int builderLength = demo(stringBuilder, N);
int bufferLength = demo(stringBuffer, N);
System.out.println("StringBuilder/StringBuffer: " + builderLength + "/" + bufferLength);
}
}
}
// Output will be something like this:
// StringBuilder/StringBuffer: 9995/10000
// StringBuilder/StringBuffer: 9996/10000
// StringBuilder/StringBuffer: 9998/10000
// StringBuilder/StringBuffer: 9997/10000
// StringBuilder/StringBuffer: 9995/10000
// StringBuilder/StringBuffer: 9996/10000
// StringBuilder/StringBuffer: 9998/10000
// StringBuilder/StringBuffer: 9998/10000
// StringBuilder/StringBuffer: 9999/10000
// StringBuilder/StringBuffer: 9999/10000
package com.ibm.javacore.collections.threadsafe;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ThreadSafeDemo {
public static int demo(final List list, final int testCount) throws InterruptedException {
ThreadGroup group = new ThreadGroup(list.getClass().getName() + "@" + list.hashCode());
final Random rand = new Random();
Runnable listAppender = new Runnable() {
public void run() {
try {
Thread.sleep(rand.nextInt(2));
} catch (InterruptedException e) {
return;
}
list.add("0");
}
};
for (int i = 0; i < testCount; i++) {
new Thread(group, listAppender, "InsertList-" + i).start();
}
while (group.activeCount() > 0) {
Thread.sleep(10);
}
return list.size();
}
public static void main(String[] args) throws InterruptedException {
List unsafeList = new ArrayList();
List safeList = Collections.synchronizedList(new ArrayList());
final int N = 10000;
for (int i = 0; i < 10; i++) {
unsafeList.clear();
safeList.clear();
int unsafeSize = demo(unsafeList, N);
int safeSize = demo(safeList, N);
System.out.println("unsafe/safe: " + unsafeSize + "/" + safeSize);
}
}
}
// Output will be something like this:
// unsafe/safe: 9999/10000
// unsafe/safe: 9997/10000
// unsafe/safe: 10000/10000
// unsafe/safe: 10000/10000
// unsafe/safe: 9999/10000
// unsafe/safe: 9999/10000
// unsafe/safe: 10000/10000
// unsafe/safe: 9998/10000
// unsafe/safe: 10000/10000
// unsafe/safe: 10000/10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment