jdk/test/java/lang/String/StringContentEqualsBug.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 17698 ab37c47ff886
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17698
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     1
/*
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     4
 *
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     8
 *
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    13
 * accompanied this code).
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    14
 *
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    18
 *
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    21
 * questions.
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    22
 */
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    23
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    24
/**
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    25
 * @test
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    26
 * @bug 8014477
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    27
 * @summary test String.contentEquals(StringBuffer)
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    28
 */
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    29
public class StringContentEqualsBug {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    30
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 17698
diff changeset
    31
    abstract static class Task extends Thread {
17698
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    32
        volatile StringBuffer sb;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    33
        volatile Exception exception;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    34
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    35
        Task(StringBuffer sb) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    36
            this.sb = sb;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    37
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    38
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    39
        @Override
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    40
        public void run() {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    41
            try {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    42
                StringBuffer sb;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    43
                while ((sb = this.sb) != null) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    44
                    doWith(sb);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    45
                }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    46
            }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    47
            catch (Exception e) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    48
                exception = e;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    49
            }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    50
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    51
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    52
        protected abstract void doWith(StringBuffer sb);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    53
    }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    54
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    55
    static class Tester extends Task {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    56
        Tester(StringBuffer sb) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    57
            super(sb);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    58
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    59
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    60
        @Override
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    61
        protected void doWith(StringBuffer sb) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    62
            "AA".contentEquals(sb);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    63
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    64
    }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    65
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    66
    static class Disturber extends Task {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    67
        Disturber(StringBuffer sb) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    68
            super(sb);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    69
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    70
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    71
        @Override
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    72
        protected void doWith(StringBuffer sb) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    73
            sb.setLength(0);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    74
            sb.trimToSize();
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    75
            sb.append("AA");
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    76
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    77
    }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    78
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    79
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    80
    public static void main(String[] args) throws Exception {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    81
        StringBuffer sb = new StringBuffer();
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    82
        Task[] tasks = new Task[3];
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    83
        (tasks[0] = new Tester(sb)).start();
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    84
        for (int i = 1; i < tasks.length; i++) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    85
            (tasks[i] = new Disturber(sb)).start();
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    86
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    87
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    88
        try
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    89
        {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    90
            // wait at most 5 seconds for any of the threads to throw exception
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    91
            for (int i = 0; i < 20; i++) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    92
                for (Task task : tasks) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    93
                    if (task.exception != null) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    94
                        throw task.exception;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    95
                    }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    96
                }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    97
                Thread.sleep(250L);
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    98
            }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
    99
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   100
        finally {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   101
            for (Task task : tasks) {
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   102
                task.sb = null;
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   103
                task.join();
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   104
            }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   105
        }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   106
    }
ab37c47ff886 8014477: (str) Race condition in String.contentEquals when comparing with StringBuffer
plevart
parents:
diff changeset
   107
}