1 /*
   2  * Copyright (c) 2002, 2017, 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 import java.net.SocketException;
  25 import java.nio.channels.DatagramChannel;
  26 import java.nio.channels.Pipe;
  27 import java.nio.channels.ServerSocketChannel;
  28 import java.nio.channels.SocketChannel;
  29 import java.nio.channels.spi.SelectorProvider;
  30 
  31 public class Open {
  32 
  33     static void test1() {
  34         for (int i=0; i<11000; i++) {
  35             try {
  36                 SocketChannel sc = SocketChannel.open();
  37             } catch (Exception e) {
  38                 // Presumably "Too many open files"
  39             }
  40         }
  41     }
  42     static void test2() {
  43         for (int i=0; i<11000; i++) {
  44             try {
  45                 DatagramChannel sc = DatagramChannel.open();
  46             } catch (Exception e) {
  47                 // Presumably "Too many open files"
  48             }
  49         }
  50     }
  51     static void test3() {
  52         SelectorProvider sp = SelectorProvider.provider();
  53         for (int i=0; i<11000; i++) {
  54             try {
  55                 Pipe p = sp.openPipe();
  56             } catch (Exception e) {
  57                 // Presumably "Too many open files"
  58             }
  59         }
  60     }
  61     static void test4() {
  62         for (int i=0; i<11000; i++) {
  63             try {
  64                 ServerSocketChannel sc = ServerSocketChannel.open();
  65             } catch (Exception e) {
  66                 // Presumably "Too many open files"
  67             }
  68         }
  69     }
  70 
  71     public static void main(String[] args) throws Exception {
  72 
  73         // Load necessary classes ahead of time
  74         DatagramChannel dc = DatagramChannel.open();
  75         Exception se = new SocketException();
  76         SelectorProvider sp = SelectorProvider.provider();
  77         Pipe p = sp.openPipe();
  78         ServerSocketChannel ssc = ServerSocketChannel.open();
  79 
  80         test1();
  81         test2();
  82         test3();
  83         test4();
  84     }
  85 
  86 }