1 /* 2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4268780 27 * @summary Collection-views of submap-views of synchronized-views of 28 * SortedMap objects do not synchronize on the correct object. 29 * (Got that?) 30 */ 31 32 package test.java.util.Collections; 33 34 import java.util.Collection; 35 import java.util.Collections; 36 import java.util.Map; 37 import java.util.SortedMap; 38 import java.util.TreeMap; 39 40 public class ViewSynch { 41 static final Integer ZERO = new Integer(0); 42 static final Int INT_ZERO = new Int(0); 43 static final Int INT_ONE = new Int(1); 44 static SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); 45 static Map m2 = m.tailMap(ZERO); 46 static Collection c = m2.values(); 47 main(String[] args)48 public static void main(String[] args) { 49 for (int i=0; i<10000; i++) 50 m.put(new Integer(i), INT_ZERO); 51 52 new Thread() { 53 public void run() { 54 for (int i=0; i<100; i++) { 55 Thread.yield(); 56 m.remove(ZERO); 57 m.put(ZERO, INT_ZERO); 58 } 59 } 60 }.start(); 61 62 c.contains(INT_ONE); 63 } 64 } 65 66 /** 67 * Like Integer, except yields while doing equals comparison, to allow 68 * for interleaving. 69 */ 70 class Int { 71 Integer x; Int(int i)72 Int(int i) {x = new Integer(i);} 73 equals(Object o)74 public boolean equals(Object o) { 75 Thread.yield(); 76 Int i = (Int)o; 77 return x.equals(i.x); 78 } 79 hashCode()80 public int hashCode() {return x.hashCode();} 81 } 82