Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/ViewSynch.java
38812 views
/*1* Copyright (c) 1999, 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 426878026* @summary Collection-views of submap-views of synchronized-views of27* SortedMap objects do not synchronize on the correct object.28* (Got that?)29*/3031import java.util.*;3233public class ViewSynch {34static final Integer ZERO = new Integer(0);35static final Int INT_ZERO = new Int(0);36static final Int INT_ONE = new Int(1);37static SortedMap m = Collections.synchronizedSortedMap(new TreeMap());38static Map m2 = m.tailMap(ZERO);39static Collection c = m2.values();4041public static void main(String[] args) {42for (int i=0; i<10000; i++)43m.put(new Integer(i), INT_ZERO);4445new Thread() {46public void run() {47for (int i=0; i<100; i++) {48Thread.yield();49m.remove(ZERO);50m.put(ZERO, INT_ZERO);51}52}53}.start();5455c.contains(INT_ONE);56}57}5859/**60* Like Integer, except yields while doing equals comparison, to allow61* for interleaving.62*/63class Int {64Integer x;65Int(int i) {x = new Integer(i);}6667public boolean equals(Object o) {68Thread.yield();69Int i = (Int)o;70return x.equals(i.x);71}7273public int hashCode() {return x.hashCode();}74}757677