RFR: 8354522: Clones of DecimalFormat cause interferences when used concurrently

Johannes Graham duke at openjdk.org
Tue Apr 15 14:15:15 UTC 2025


On Fri, 11 Apr 2025 17:31:08 GMT, Johannes Graham <duke at openjdk.org> wrote:

> The `DigitList` class used in `DecimalFormat` does not reset the `data` array in its clone method. This can cause interference when clones are used concurrently.

Reproducing code example:


public class DecimalFormatTest {
    static AtomicInteger mismatchCount = new AtomicInteger(0);

    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("#");
        String str = df.format(Math.PI); // initial use of formatter
        System.out.println(str);

        try (var ex = Executors.newThreadPerTaskExecutor(Thread.ofPlatform().factory())) {
            for (int i = 0; i < 50; i++) {
                // each thread gets its own clone of df
                DecimalFormat threadDf = (DecimalFormat) df.clone();
                ex.execute(makeTask(threadDf));
            }
        }

        System.out.println("mismatchCount = " + mismatchCount);
    }

    private static Runnable makeTask(DecimalFormat threadDf) {
        return () -> {
            for (int i = 0; i < 1000000; i++) {
                String dfString = threadDf.format(BigDecimal.valueOf(i));
                String str = String.valueOf(i);
                if (!str.equals(dfString)) {
                    System.err.println("mismatch: str = " + str + " dfString = " + dfString);
                    mismatchCount.incrementAndGet();
                }
            }
        };
    }
}

-------------

PR Comment: https://git.openjdk.org/jdk/pull/24598#issuecomment-2797607528


More information about the core-libs-dev mailing list