J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 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 4486658 |
| 27 | * @summary thread safety of toArray methods of subCollections |
| 28 | * @author Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | |
| 34 | public class toArray { |
| 35 | |
| 36 | public static void main(String[] args) throws Throwable { |
| 37 | final Throwable throwable[] = new Throwable[1]; |
| 38 | final int maxSize = 1000; |
| 39 | final ConcurrentHashMap<Integer, Integer> m |
| 40 | = new ConcurrentHashMap<Integer, Integer>(); |
| 41 | |
| 42 | final Thread t1 = new Thread() { public void run() { |
| 43 | for (int i = 0; i < maxSize; i++) |
| 44 | m.put(i,i);}}; |
| 45 | |
| 46 | final Thread t2 = new Thread() { |
| 47 | public Throwable exception = null; |
| 48 | private int prevSize = 0; |
| 49 | |
| 50 | private boolean checkProgress(Object[] a) { |
| 51 | int size = a.length; |
| 52 | if (size < prevSize) throw new RuntimeException("WRONG WAY"); |
| 53 | if (size > maxSize) throw new RuntimeException("OVERSHOOT"); |
| 54 | if (size == maxSize) return true; |
| 55 | prevSize = size; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | public void run() { |
| 60 | try { |
| 61 | Integer[] empty = new Integer[0]; |
| 62 | while (true) { |
| 63 | if (checkProgress(m.values().toArray())) return; |
| 64 | if (checkProgress(m.keySet().toArray())) return; |
| 65 | if (checkProgress(m.values().toArray(empty))) return; |
| 66 | if (checkProgress(m.keySet().toArray(empty))) return; |
| 67 | } |
| 68 | } catch (Throwable t) { |
| 69 | throwable[0] = t; |
| 70 | }}}; |
| 71 | |
| 72 | t2.start(); |
| 73 | t1.start(); |
| 74 | |
| 75 | t1.join(); |
| 76 | t2.join(); |
| 77 | |
| 78 | if (throwable[0] != null) |
| 79 | throw throwable[0]; |
| 80 | } |
| 81 | } |