public static StringBuilder doTest() { SynchSB thesb = allocate(SynchSB.class); { StringBuilder sb = allocate(StringBuilder.class); sb.value = new char[16]; thesb.sb = sb; } for (int i = 0; i < 10000; i++) { synchronized (thesb) { StringBuilder sb = thesb.sb; String str = "abc"; int len = 3; // str.value.length; int minimalCapacity = sb.count + len; if (minimalCapacity - sb.value.length > 0) { { int newCapacity = sb.value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } { char[] original = sb.value; char[] copy = new char[newCapacity]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newCapacity)); sb.value = copy; } } } System.arraycopy(str.value, 0, sb.value, sb.count, len); sb.count += len; } int sblen; synchronized (thesb) { sblen = thesb.sb.length; } synchronized (thesb) { StringBuilder sb = thesb.sb; System.arraycopy(sb.value, sblen, sb.value, 0, 0); sb.count = 0; } } return thesb.sb; }