下面列出了java.nio.channels.spi.AbstractSelectableChannel#keyFor() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean
isPaused(
AbstractSelectableChannel channel )
{
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
return(( key.interestOps() & INTEREST_OP ) == 0 );
}else{
try{
register_cancel_list_mon.enter();
Boolean b = paused_states.get( channel );
return( b != null && b );
}finally{
register_cancel_list_mon.exit();
}
}
}
public void pauseSelects( AbstractSelectableChannel channel ) {
//System.out.println( "pauseSelects: " + channel + " - " + Debug.getCompressedStackTrace() );
if( channel == null ) {
return;
}
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
key.interestOps( key.interestOps() & ~INTEREST_OP );
}
else { //channel not (yet?) registered
if( channel.isOpen() ) { //only bother if channel has not already been closed
try{ register_cancel_list_mon.enter();
paused_states.put( channel, Boolean.TRUE); //ensure the op is paused upon reg select-time reg
}
finally{ register_cancel_list_mon.exit(); }
}
}
}
public boolean
isRegistered( AbstractSelectableChannel channel ){
SelectionKey key = channel.keyFor( selector );
if( key != null ){
return( true );
}else{
try{
register_cancel_list_mon.enter();
// ensure that there's only one operation outstanding for a given channel
// at any one time (the latest operation requested )
return( register_cancel_list.containsKey( channel ));
}finally{
register_cancel_list_mon.exit();
}
}
}
public void pauseSelects( AbstractSelectableChannel channel ) {
//System.out.println( "pauseSelects: " + channel + " - " + Debug.getCompressedStackTrace() );
if( channel == null ) {
return;
}
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
key.interestOps( key.interestOps() & ~INTEREST_OP );
}
else { //channel not (yet?) registered
if( channel.isOpen() ) { //only bother if channel has not already been closed
try{ register_cancel_list_mon.enter();
paused_states.put( channel, new Boolean( true ) ); //ensure the op is paused upon reg select-time reg
}
finally{ register_cancel_list_mon.exit(); }
}
}
}
public void resumeSelects( AbstractSelectableChannel channel ) {
//System.out.println( "resumeSelects: " + channel + " - " + Debug.getCompressedStackTrace() );
if( channel == null ) {
Debug.printStackTrace( new Exception( "resumeSelects():: channel == null" ) );
return;
}
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
// if we're resuming a non-interested key then reset the metrics
if (( key.interestOps() & INTEREST_OP ) == 0 ){
RegistrationData data = (RegistrationData)key.attachment();
data.last_select_success_time = SystemTime.getCurrentTime();
data.non_progress_count = 0;
}
key.interestOps( key.interestOps() | INTEREST_OP );
}
else { //channel not (yet?) registered
try{ register_cancel_list_mon.enter();
paused_states.remove( channel ); //check if the channel's op has been already paused before select-time reg
}
finally{ register_cancel_list_mon.exit(); }
}
//try{
// selector.wakeup();
//}
//catch( Throwable t ) { Debug.out( "selector.wakeup():: caught exception: ", t ); }
}
public void resumeSelects( AbstractSelectableChannel channel ) {
//System.out.println( "resumeSelects: " + channel + " - " + Debug.getCompressedStackTrace() );
if( channel == null ) {
Debug.printStackTrace( new Exception( "resumeSelects():: channel == null" ) );
return;
}
SelectionKey key = channel.keyFor( selector );
if( key != null && key.isValid() ) {
// if we're resuming a non-interested key then reset the metrics
if (( key.interestOps() & INTEREST_OP ) == 0 ){
RegistrationData data = (RegistrationData)key.attachment();
data.last_select_success_time = SystemTime.getCurrentTime();
data.non_progress_count = 0;
}
key.interestOps( key.interestOps() | INTEREST_OP );
}
else { //channel not (yet?) registered
try{ register_cancel_list_mon.enter();
paused_states.remove( channel ); //check if the channel's op has been already paused before select-time reg
}
finally{ register_cancel_list_mon.exit(); }
}
//try{
// selector.wakeup();
//}
//catch( Throwable t ) { Debug.out( "selector.wakeup():: caught exception: ", t ); }
}
protected void close(AbstractSelectableChannel channel) {
if (channel == null)
return;
try {
SelectionKey key = channel.keyFor(selector);
if (key != null)
key.cancel();
channel.close();
} catch (Exception e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
}
}