All Downloads are FREE. Search and download functionalities are using the official Maven repository.

examples.StringBufferLockElision Maven / Gradle / Ivy

import java.util.Vector;
import java.util.Random;

// See http://www.infoq.com/articles/java-threading-optimizations-p2

public class StringBufferLockElision
{
    private java.util.Random random = new java.util.Random();

    public StringBufferLockElision()
    {
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < 500_000; i++)
        {
            String joined = concatPieces("a", "b", "c");

            buffer.append(joined);
        }

        System.out.println(buffer.toString());
    }

    private String concatPieces(String one, String two, String three)
    {
        StringBuffer buffer = new StringBuffer();

        buffer.append(one);
        buffer.append(two);
        buffer.append(three);

        return buffer.toString();
    }

    public static void main(String[] args)
    {
        new StringBufferLockElision();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy