public class StrTest {
public static void main(String[] args) {
strBufferTest();
strBuilderTest();
}
private static void strBufferTest() {
Date start = new Date();
StringBuffer stringBuffer = new StringBuffer();
new Thread(() -> {
IntStream.range(1, 10000).forEach(num -> {
stringBuffer.append(num);
});
}).start();
new Thread(() -> {
IntStream.range(1, 10000).forEach(num -> {
stringBuffer.append(num);
});
}).start();
new Thread(() -> {
Date end = new Date();
long millis = end.getTime() - start.getTime();
try {
Thread.sleep(5000);
System.out.println("StringBuffer length : " + stringBuffer.length());
System.out.println("StringBuffer time(ms) :" + millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
private static void strBuilderTest() {
Date start = new Date();
StringBuilder stringBuilder = new StringBuilder();
new Thread(() -> {
IntStream.range(1, 10000).forEach(num -> {
stringBuilder.append(num);
});
}).start();
new Thread(() -> {
IntStream.range(1, 10000).forEach(num -> {
stringBuilder.append(num);
});
}).start();
new Thread(() -> {
Date end = new Date();
long millis = end.getTime() - start.getTime();
try {
Thread.sleep(5000);
System.out.println("StringBuilder length : " + stringBuilder.length());
System.out.println("StringBuilder time(ms) :" + millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}