Path: blob/master/test/jdk/java/util/Collections/ViewSynch.java
41149 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.Collection;32import java.util.Collections;33import java.util.Map;34import java.util.SortedMap;35import java.util.TreeMap;3637public class ViewSynch {38static final Integer ZERO = new Integer(0);39static final Int INT_ZERO = new Int(0);40static final Int INT_ONE = new Int(1);41static SortedMap m = Collections.synchronizedSortedMap(new TreeMap());42static Map m2 = m.tailMap(ZERO);43static Collection c = m2.values();4445public static void main(String[] args) {46for (int i=0; i<10000; i++)47m.put(new Integer(i), INT_ZERO);4849new Thread() {50public void run() {51for (int i=0; i<100; i++) {52Thread.yield();53m.remove(ZERO);54m.put(ZERO, INT_ZERO);55}56}57}.start();5859c.contains(INT_ONE);60}61}6263/**64* Like Integer, except yields while doing equals comparison, to allow65* for interleaving.66*/67class Int {68Integer x;69Int(int i) {x = new Integer(i);}7071public boolean equals(Object o) {72Thread.yield();73Int i = (Int)o;74return x.equals(i.x);75}7677public int hashCode() {return x.hashCode();}78}798081