src/java.base/share/classes/java/nio/channels/Selector.java
changeset 49802 8ac08fa69f00
parent 47216 71c04702a3d5
child 50602 ed8de3d0cd28
equal deleted inserted replaced
49801:466d4df248a6 49802:8ac08fa69f00
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    82  *
    82  *
    83  * <a id="sks"></a><p> Keys are added to the selected-key set by selection
    83  * <a id="sks"></a><p> Keys are added to the selected-key set by selection
    84  * operations.  A key may be removed directly from the selected-key set by
    84  * operations.  A key may be removed directly from the selected-key set by
    85  * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
    85  * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
    86  * method or by invoking the {@link java.util.Iterator#remove() remove} method
    86  * method or by invoking the {@link java.util.Iterator#remove() remove} method
    87  * of an {@link java.util.Iterator iterator} obtained from the
    87  * of an {@link java.util.Iterator iterator} obtained from the set.
    88  * set.  Keys are never removed from the selected-key set in any other way;
    88  * All keys may be removed from the selected-key set by invoking the set's
    89  * they are not, in particular, removed as a side effect of selection
    89  * {@link java.util.Set#clear() clear} method.  Keys may not be added directly
    90  * operations.  Keys may not be added directly to the selected-key set. </p>
    90  * to the selected-key set. </p>
    91  *
       
    92  *
    91  *
    93  * <a id="selop"></a>
    92  * <a id="selop"></a>
    94  * <h2>Selection</h2>
    93  * <h2>Selection</h2>
    95  *
    94  *
    96  * <p> During each selection operation, keys may be added to and removed from a
    95  * <p> During each selection operation, keys may be added to and removed from a
   142  * difference between the three selection methods. </p>
   141  * difference between the three selection methods. </p>
   143  *
   142  *
   144  *
   143  *
   145  * <h2>Concurrency</h2>
   144  * <h2>Concurrency</h2>
   146  *
   145  *
   147  * <p> Selectors are themselves safe for use by multiple concurrent threads;
   146  * <p> A Selector and its key set are safe for use by multiple concurrent
   148  * their key sets, however, are not.
   147  * threads.  Its selected-key set and cancelled-key set, however, are not.
   149  *
   148  *
   150  * <p> The selection operations synchronize on the selector itself, on the key
   149  * <p> The selection operations synchronize on the selector itself, on the
   151  * set, and on the selected-key set, in that order.  They also synchronize on
   150  * selected-key set, in that order.  They also synchronize on the cancelled-key
   152  * the cancelled-key set during steps (1) and (3) above.
   151  * set during steps (1) and (3) above.
   153  *
   152  *
   154  * <p> Changes made to the interest sets of a selector's keys while a
   153  * <p> Changes made to the interest sets of a selector's keys while a
   155  * selection operation is in progress have no effect upon that operation; they
   154  * selection operation is in progress have no effect upon that operation; they
   156  * will be seen by the next selection operation.
   155  * will be seen by the next selection operation.
   157  *
   156  *
   178  *   interrupt status will be set and the selector's {@link #wakeup wakeup}
   177  *   interrupt status will be set and the selector's {@link #wakeup wakeup}
   179  *   method will be invoked. </p></li>
   178  *   method will be invoked. </p></li>
   180  *
   179  *
   181  * </ul>
   180  * </ul>
   182  *
   181  *
   183  * <p> The {@link #close close} method synchronizes on the selector and all
   182  * <p> The {@link #close close} method synchronizes on the selector and its
   184  * three key sets in the same order as in a selection operation.
   183  * selected-key set in the same order as in a selection operation.
   185  *
   184  *
   186  * <a id="ksc"></a>
   185  * <a id="ksc"></a>
   187  *
   186  * <p> A Selector's key set is safe for use by multiple concurrent threads.
   188  * <p> A selector's key and selected-key sets are not, in general, safe for use
   187  * Retrieval operations from the key set do not generally block and so may
   189  * by multiple concurrent threads.  If such a thread might modify one of these
   188  * overlap with new registrations that add to the set, or with the cancellation
   190  * sets directly then access should be controlled by synchronizing on the set
   189  * steps of selection operations that remove keys from the set.  Iterators and
   191  * itself.  The iterators returned by these sets' {@link
   190  * spliterators return elements reflecting the state of the set at some point at
   192  * java.util.Set#iterator() iterator} methods are <i>fail-fast:</i> If the set
   191  * or since the creation of the iterator/spliterator.  They do not throw
   193  * is modified after the iterator is created, in any way except by invoking the
   192  * {@link java.util.ConcurrentModificationException ConcurrentModificationException}.
   194  * iterator's own {@link java.util.Iterator#remove() remove} method, then a
   193  *
   195  * {@link java.util.ConcurrentModificationException} will be thrown. </p>
   194  * <a id="sksc"></a>
   196  *
   195  * <p> A selector's selected-key set is not, in general, safe for use by
       
   196  * multiple concurrent threads.  If such a thread might modify the set directly
       
   197  * then access should be controlled by synchronizing on the set itself.  The
       
   198  * iterators returned by the set's {@link java.util.Set#iterator() iterator}
       
   199  * methods are <i>fail-fast:</i> If the set is modified after the iterator is
       
   200  * created, in any way except by invoking the iterator's own {@link
       
   201  * java.util.Iterator#remove() remove} method, then a {@link
       
   202  * java.util.ConcurrentModificationException} will be thrown. </p>
   197  *
   203  *
   198  * @author Mark Reinhold
   204  * @author Mark Reinhold
   199  * @author JSR-51 Expert Group
   205  * @author JSR-51 Expert Group
   200  * @since 1.4
   206  * @since 1.4
   201  *
   207  *
   247      * <p> The key set is not directly modifiable.  A key is removed only after
   253      * <p> The key set is not directly modifiable.  A key is removed only after
   248      * it has been cancelled and its channel has been deregistered.  Any
   254      * it has been cancelled and its channel has been deregistered.  Any
   249      * attempt to modify the key set will cause an {@link
   255      * attempt to modify the key set will cause an {@link
   250      * UnsupportedOperationException} to be thrown.
   256      * UnsupportedOperationException} to be thrown.
   251      *
   257      *
   252      * <p> The key set is <a href="#ksc">not thread-safe</a>. </p>
   258      * <p> The set is <a href="#ksc">safe</a> for use by multiple concurrent
       
   259      * threads.  </p>
   253      *
   260      *
   254      * @return  This selector's key set
   261      * @return  This selector's key set
   255      *
   262      *
   256      * @throws  ClosedSelectorException
   263      * @throws  ClosedSelectorException
   257      *          If this selector is closed
   264      *          If this selector is closed
   263      *
   270      *
   264      * <p> Keys may be removed from, but not directly added to, the
   271      * <p> Keys may be removed from, but not directly added to, the
   265      * selected-key set.  Any attempt to add an object to the key set will
   272      * selected-key set.  Any attempt to add an object to the key set will
   266      * cause an {@link UnsupportedOperationException} to be thrown.
   273      * cause an {@link UnsupportedOperationException} to be thrown.
   267      *
   274      *
   268      * <p> The selected-key set is <a href="#ksc">not thread-safe</a>. </p>
   275      * <p> The selected-key set is <a href="#sksc">not thread-safe</a>.  </p>
   269      *
   276      *
   270      * @return  This selector's selected-key set
   277      * @return  This selector's selected-key set
   271      *
   278      *
   272      * @throws  ClosedSelectorException
   279      * @throws  ClosedSelectorException
   273      *          If this selector is closed
   280      *          If this selector is closed
   324      *          If this selector is closed
   331      *          If this selector is closed
   325      *
   332      *
   326      * @throws  IllegalArgumentException
   333      * @throws  IllegalArgumentException
   327      *          If the value of the timeout argument is negative
   334      *          If the value of the timeout argument is negative
   328      */
   335      */
   329     public abstract int select(long timeout)
   336     public abstract int select(long timeout) throws IOException;
   330         throws IOException;
       
   331 
   337 
   332     /**
   338     /**
   333      * Selects a set of keys whose corresponding channels are ready for I/O
   339      * Selects a set of keys whose corresponding channels are ready for I/O
   334      * operations.
   340      * operations.
   335      *
   341      *