0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4486658
0N/A * @summary thread safety of toArray methods of subCollections
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/A
0N/Apublic class toArray {
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A final Throwable throwable[] = new Throwable[1];
0N/A final int maxSize = 1000;
0N/A final ConcurrentHashMap<Integer, Integer> m
0N/A = new ConcurrentHashMap<Integer, Integer>();
0N/A
0N/A final Thread t1 = new Thread() { public void run() {
0N/A for (int i = 0; i < maxSize; i++)
0N/A m.put(i,i);}};
0N/A
0N/A final Thread t2 = new Thread() {
0N/A public Throwable exception = null;
0N/A private int prevSize = 0;
0N/A
0N/A private boolean checkProgress(Object[] a) {
0N/A int size = a.length;
0N/A if (size < prevSize) throw new RuntimeException("WRONG WAY");
0N/A if (size > maxSize) throw new RuntimeException("OVERSHOOT");
0N/A if (size == maxSize) return true;
0N/A prevSize = size;
0N/A return false;
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A Integer[] empty = new Integer[0];
0N/A while (true) {
0N/A if (checkProgress(m.values().toArray())) return;
0N/A if (checkProgress(m.keySet().toArray())) return;
0N/A if (checkProgress(m.values().toArray(empty))) return;
0N/A if (checkProgress(m.keySet().toArray(empty))) return;
0N/A }
0N/A } catch (Throwable t) {
0N/A throwable[0] = t;
0N/A }}};
0N/A
0N/A t2.start();
0N/A t1.start();
0N/A
0N/A t1.join();
0N/A t2.join();
0N/A
0N/A if (throwable[0] != null)
0N/A throw throwable[0];
0N/A }
0N/A}