duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | import java.util.*; |
| 33 | |
| 34 | public class ViewSynch { |
| 35 | static final Integer ZERO = new Integer(0); |
| 36 | static final Int INT_ZERO = new Int(0); |
| 37 | static final Int INT_ONE = new Int(1); |
| 38 | static SortedMap m = Collections.synchronizedSortedMap(new TreeMap()); |
| 39 | static Map m2 = m.tailMap(ZERO); |
| 40 | static Collection c = m2.values(); |
| 41 | |
| 42 | public static void main(String[] args) { |
| 43 | for (int i=0; i<10000; i++) |
| 44 | m.put(new Integer(i), INT_ZERO); |
| 45 | |
| 46 | new Thread() { |
| 47 | public void run() { |
| 48 | for (int i=0; i<100; i++) { |
| 49 | Thread.yield(); |
| 50 | m.remove(ZERO); |
| 51 | m.put(ZERO, INT_ZERO); |
| 52 | } |
| 53 | } |
| 54 | }.start(); |
| 55 | |
| 56 | c.contains(INT_ONE); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Like Integer, except yields while doing equals comparison, to allow |
| 62 | * for interleaving. |
| 63 | */ |
| 64 | class Int { |
| 65 | Integer x; |
| 66 | Int(int i) {x = new Integer(i);} |
| 67 | |
| 68 | public boolean equals(Object o) { |
| 69 | Thread.yield(); |
| 70 | Int i = (Int)o; |
| 71 | return x.equals(i.x); |
| 72 | } |
| 73 | |
| 74 | public int hashCode() {return x.hashCode();} |
| 75 | } |