Path: blob/master/test/jdk/java/lang/String/StringContentEqualsBug.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 801447726* @summary test String.contentEquals(StringBuffer)27*/28public class StringContentEqualsBug {2930abstract static class Task extends Thread {31volatile StringBuffer sb;32volatile Exception exception;3334Task(StringBuffer sb) {35this.sb = sb;36}3738@Override39public void run() {40try {41StringBuffer sb;42while ((sb = this.sb) != null) {43doWith(sb);44}45}46catch (Exception e) {47exception = e;48}49}5051protected abstract void doWith(StringBuffer sb);52}5354static class Tester extends Task {55Tester(StringBuffer sb) {56super(sb);57}5859@Override60protected void doWith(StringBuffer sb) {61"AA".contentEquals(sb);62}63}6465static class Disturber extends Task {66Disturber(StringBuffer sb) {67super(sb);68}6970@Override71protected void doWith(StringBuffer sb) {72sb.setLength(0);73sb.trimToSize();74sb.append("AA");75}76}777879public static void main(String[] args) throws Exception {80StringBuffer sb = new StringBuffer();81Task[] tasks = new Task[3];82(tasks[0] = new Tester(sb)).start();83for (int i = 1; i < tasks.length; i++) {84(tasks[i] = new Disturber(sb)).start();85}8687try88{89// wait at most 5 seconds for any of the threads to throw exception90for (int i = 0; i < 20; i++) {91for (Task task : tasks) {92if (task.exception != null) {93throw task.exception;94}95}96Thread.sleep(250L);97}98}99finally {100for (Task task : tasks) {101task.sb = null;102task.join();103}104}105}106}107108109