Path: blob/master/test/jdk/java/nio/channels/Selector/HelperSlowToDie.java
41153 views
/*1* Copyright (c) 2009, 2010, 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 682360926* @summary Selector.select can hangs on Windows for cases where a helper thread27* becomes redudant but a new helper is immediately needed.28*/2930import java.nio.channels.*;31import java.io.IOException;3233public class HelperSlowToDie {34private static final int CHANNELS_PER_THREAD = 1023;35private static final int TEST_ITERATIONS = 200;36private static volatile boolean done;3738public static void main(String[] args) throws IOException {39if (!System.getProperty("os.name").startsWith("Windows")) {40System.out.println("Test skipped as it verifies a Windows specific bug");41return;42}4344Selector sel = Selector.open();4546// register channels47SocketChannel[] channels = new SocketChannel[CHANNELS_PER_THREAD];48for (int i=0; i<CHANNELS_PER_THREAD; i++) {49SocketChannel sc = SocketChannel.open();50sc.configureBlocking(false);51sc.register(sel, SelectionKey.OP_CONNECT);52channels[i] = sc;53}54sel.selectNow();5556// Start threads to swamp all cores but one. This improves the chances57// of duplicating the bug.58Runnable busy = new Runnable() {59public void run() {60while (!done) ; // no nothing61}62};63int ncores = Runtime.getRuntime().availableProcessors();64for (int i=0; i<ncores-1; i++)65new Thread(busy).start();6667// Loop changing the number of channels from 1023 to 1024 and back.68for (int i=0; i<TEST_ITERATIONS; i++) {69SocketChannel sc = SocketChannel.open();70sc.configureBlocking(false);71sc.register(sel, SelectionKey.OP_CONNECT);72sel.selectNow(); // cause helper to spin up73sc.close();74sel.selectNow(); // cause helper to retire75}7677// terminate busy threads78done = true;7980// clean-up81for (int i=0; i<CHANNELS_PER_THREAD; i++) {82channels[i].close();83}84sel.close();85}86}878889