src/java.base/share/classes/java/nio/channels/spi/AbstractSelectableChannel.java
changeset 59146 455612b3161a
parent 49802 8ac08fa69f00
equal deleted inserted replaced
59145:ea044aedc2b6 59146:455612b3161a
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, 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
    32 import java.nio.channels.IllegalBlockingModeException;
    32 import java.nio.channels.IllegalBlockingModeException;
    33 import java.nio.channels.IllegalSelectorException;
    33 import java.nio.channels.IllegalSelectorException;
    34 import java.nio.channels.SelectableChannel;
    34 import java.nio.channels.SelectableChannel;
    35 import java.nio.channels.SelectionKey;
    35 import java.nio.channels.SelectionKey;
    36 import java.nio.channels.Selector;
    36 import java.nio.channels.Selector;
       
    37 import java.util.Arrays;
       
    38 import java.util.function.Consumer;
    37 
    39 
    38 
    40 
    39 /**
    41 /**
    40  * Base implementation class for selectable channels.
    42  * Base implementation class for selectable channels.
    41  *
    43  *
   166     }
   168     }
   167 
   169 
   168     public final SelectionKey keyFor(Selector sel) {
   170     public final SelectionKey keyFor(Selector sel) {
   169         synchronized (keyLock) {
   171         synchronized (keyLock) {
   170             return findKey(sel);
   172             return findKey(sel);
       
   173         }
       
   174     }
       
   175 
       
   176     /**
       
   177      * Invokes an action for each key.
       
   178      *
       
   179      * This method is invoked by DatagramChannelImpl::disconnect.
       
   180      */
       
   181     private void forEach(Consumer<SelectionKey> action) {
       
   182         synchronized (keyLock) {
       
   183             SelectionKey[] keys = this.keys;
       
   184             if (keys != null) {
       
   185                 Arrays.stream(keys).filter(k -> k != null).forEach(action::accept);
       
   186             }
   171         }
   187         }
   172     }
   188     }
   173 
   189 
   174     /**
   190     /**
   175      * Registers this channel with the given selector, returning a selection key.
   191      * Registers this channel with the given selector, returning a selection key.