Java Doc for Selector.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.channels.Selector

All known Subclasses:   java.nio.channels.spi.AbstractSelector,
Selector
abstract public class Selector (Code)
A multiplexor of SelectableChannel objects.

A selector may be created by invoking the Selector.open open method of this class, which will use the system's default java.nio.channels.spi.SelectorProvider selector provider to create a new selector. A selector may also be created by invoking the java.nio.channels.spi.SelectorProvider.openSelector openSelector method of a custom selector provider. A selector remains open until it is closed via its Selector.close close method.

A selectable channel's registration with a selector is represented by a SelectionKey object. A selector maintains three sets of selection keys:

  • The key set contains the keys representing the current channel registrations of this selector. This set is returned by the Selector.keys() keys method.

  • The selected-key set is the set of keys such that each key's channel was detected to be ready for at least one of the operations identified in the key's interest set during a prior selection operation. This set is returned by the Selector.selectedKeys() selectedKeys method. The selected-key set is always a subset of the key set.

  • The cancelled-key set is the set of keys that have been cancelled but whose channels have not yet been deregistered. This set is not directly accessible. The cancelled-key set is always a subset of the key set.

All three sets are empty in a newly-created selector.

A key is added to a selector's key set as a side effect of registering a channel via the channel's SelectableChannel.register(Selectorint)register method. Cancelled keys are removed from the key set during selection operations. The key set itself is not directly modifiable.

A key is added to its selector's cancelled-key set when it is cancelled, whether by closing its channel or by invoking its SelectionKey.cancelcancel method. Cancelling a key will cause its channel to be deregistered during the next selection operation, at which time the key will removed from all of the selector's key sets.

Keys are added to the selected-key set by selection operations. A key may be removed directly from the selected-key set by invoking the set's java.util.Set.remove(java.lang.Object) remove method or by invoking the java.util.Iterator.remove remove method of an java.util.Iterator iterator obtained from the set. Keys are never removed from the selected-key set in any other way; they are not, in particular, removed as a side effect of selection operations. Keys may not be added directly to the selected-key set.

Selection

During each selection operation, keys may be added to and removed from a selector's selected-key set and may be removed from its key and cancelled-key sets. Selection is performed by the Selector.select() , Selector.select(long) , and Selector.selectNow() methods, and involves three steps:

  1. Each key in the cancelled-key set is removed from each key set of which it is a member, and its channel is deregistered. This step leaves the cancelled-key set empty.

  2. The underlying operating system is queried for an update as to the readiness of each remaining channel to perform any of the operations identified by its key's interest set as of the moment that the selection operation began. For a channel that is ready for at least one such operation, one of the following two actions is performed:

    1. If the channel's key is not already in the selected-key set then it is added to that set and its ready-operation set is modified to identify exactly those operations for which the channel is now reported to be ready. Any readiness information previously recorded in the ready set is discarded.

    2. Otherwise the channel's key is already in the selected-key set, so its ready-operation set is modified to identify any new operations for which the channel is reported to be ready. Any readiness information previously recorded in the ready set is preserved; in other words, the ready set returned by the underlying system is bitwise-disjoined into the key's current ready set.

  3. If all of the keys in the key set at the start of this step have empty interest sets then neither the selected-key set nor any of the keys' ready-operation sets will be updated.
  4. If any keys were added to the cancelled-key set while step (2) was in progress then they are processed as in step (1).

Whether or not a selection operation blocks to wait for one or more channels to become ready, and if so for how long, is the only essential difference between the three selection methods.

Concurrency

Selectors are themselves safe for use by multiple concurrent threads; their key sets, however, are not.

The selection operations synchronize on the selector itself, on the key set, and on the selected-key set, in that order. They also synchronize on the cancelled-key set during steps (1) and (3) above.

Changes made to the interest sets of a selector's keys while a selection operation is in progress have no effect upon that operation; they will be seen by the next selection operation.

Keys may be cancelled and channels may be closed at any time. Hence the presence of a key in one or more of a selector's key sets does not imply that the key is valid or that its channel is open. Application code should be careful to synchronize and check these conditions as necessary if there is any possibility that another thread will cancel a key or close a channel.

A thread blocked in one of the Selector.select() or Selector.select(long) methods may be interrupted by some other thread in one of three ways:

The Selector.close close method synchronizes on the selector and all three key sets in the same order as in a selection operation.

A selector's key and selected-key sets are not, in general, safe for use by multiple concurrent threads. If such a thread might modify one of these sets directly then access should be controlled by synchronizing on the set itself. The iterators returned by these sets' java.util.Set.iterator iterator methods are fail-fast: If the set is modified after the iterator is created, in any way except by invoking the iterator's own java.util.Iterator.remove remove method, then a java.util.ConcurrentModificationException will be thrown.


author:
   Mark Reinhold
author:
   JSR-51 Expert Group
version:
   1.44, 07/05/05
since:
   1.4
See Also:   SelectableChannel
See Also:   SelectionKey



Constructor Summary
protected  Selector()
     Initializes a new instance of this class.

Method Summary
abstract public  voidclose()
     Closes this selector.

If a thread is currently blocked in one of this selector's selection methods then it is interrupted as if by invoking the selector's Selector.wakeup wakeup method.

Any uncancelled keys still associated with this selector are invalidated, their channels are deregistered, and any other resources associated with this selector are released.

If this selector is already closed then invoking this method has no effect.

After a selector is closed, any further attempt to use it, except by invoking this method or the Selector.wakeup wakeup method, will cause a ClosedSelectorException to be thrown.

abstract public  booleanisOpen()
     Tells whether or not this selector is open.
abstract public  Set<SelectionKey>keys()
     Returns this selector's key set.

The key set is not directly modifiable.

public static  Selectoropen()
     Opens a selector.

The new selector is created by invoking the java.nio.channels.spi.SelectorProvider.openSelector openSelector method of the system-wide default java.nio.channels.spi.SelectorProvider object.

abstract public  SelectorProviderprovider()
     Returns the provider that created this channel.
abstract public  intselect(long timeout)
     Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation.

abstract public  intselect()
     Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation.

abstract public  intselectNow()
     Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a non-blocking selection operation.

abstract public  Set<SelectionKey>selectedKeys()
     Returns this selector's selected-key set.

Keys may be removed from, but not directly added to, the selected-key set.

abstract public  Selectorwakeup()
     Causes the first selection operation that has not yet returned to return immediately.

If another thread is currently blocked in an invocation of the Selector.select() or Selector.select(long) methods then that invocation will return immediately.



Constructor Detail
Selector
protected Selector()(Code)
Initializes a new instance of this class.




Method Detail
close
abstract public void close() throws IOException(Code)
Closes this selector.

If a thread is currently blocked in one of this selector's selection methods then it is interrupted as if by invoking the selector's Selector.wakeup wakeup method.

Any uncancelled keys still associated with this selector are invalidated, their channels are deregistered, and any other resources associated with this selector are released.

If this selector is already closed then invoking this method has no effect.

After a selector is closed, any further attempt to use it, except by invoking this method or the Selector.wakeup wakeup method, will cause a ClosedSelectorException to be thrown.


throws:
  IOException - If an I/O error occurs



isOpen
abstract public boolean isOpen()(Code)
Tells whether or not this selector is open.

true if, and only if, this selector is open



keys
abstract public Set<SelectionKey> keys()(Code)
Returns this selector's key set.

The key set is not directly modifiable. A key is removed only after it has been cancelled and its channel has been deregistered. Any attempt to modify the key set will cause an UnsupportedOperationException to be thrown.

The key set is not thread-safe.

This selector's key set
throws:
  ClosedSelectorException - If this selector is closed



open
public static Selector open() throws IOException(Code)
Opens a selector.

The new selector is created by invoking the java.nio.channels.spi.SelectorProvider.openSelector openSelector method of the system-wide default java.nio.channels.spi.SelectorProvider object.

A new selector
throws:
  IOException - If an I/O error occurs



provider
abstract public SelectorProvider provider()(Code)
Returns the provider that created this channel.

The provider that created this channel



select
abstract public int select(long timeout) throws IOException(Code)
Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's Selector.wakeup wakeup method is invoked, the current thread is interrupted, or the given timeout period expires, whichever comes first.

This method does not offer real-time guarantees: It schedules the timeout as if by invoking the Object.wait(long) method.


Parameters:
  timeout - If positive, block for up to timeoutmilliseconds, more or less, while waiting for achannel to become ready; if zero, block indefinitely;must not be negative The number of keys, possibly zero,whose ready-operation sets were updated
throws:
  IOException - If an I/O error occurs
throws:
  ClosedSelectorException - If this selector is closed
throws:
  IllegalArgumentException - If the value of the timeout argument is negative



select
abstract public int select() throws IOException(Code)
Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's Selector.wakeup wakeup method is invoked, or the current thread is interrupted, whichever comes first.

The number of keys, possibly zero,whose ready-operation sets were updated
throws:
  IOException - If an I/O error occurs
throws:
  ClosedSelectorException - If this selector is closed



selectNow
abstract public int selectNow() throws IOException(Code)
Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a non-blocking selection operation. If no channels have become selectable since the previous selection operation then this method immediately returns zero.

Invoking this method clears the effect of any previous invocations of the Selector.wakeup wakeup method.

The number of keys, possibly zero, whose ready-operation setswere updated by the selection operation
throws:
  IOException - If an I/O error occurs
throws:
  ClosedSelectorException - If this selector is closed



selectedKeys
abstract public Set<SelectionKey> selectedKeys()(Code)
Returns this selector's selected-key set.

Keys may be removed from, but not directly added to, the selected-key set. Any attempt to add an object to the key set will cause an UnsupportedOperationException to be thrown.

The selected-key set is not thread-safe.

This selector's selected-key set
throws:
  ClosedSelectorException - If this selector is closed



wakeup
abstract public Selector wakeup()(Code)
Causes the first selection operation that has not yet returned to return immediately.

If another thread is currently blocked in an invocation of the Selector.select() or Selector.select(long) methods then that invocation will return immediately. If no selection operation is currently in progress then the next invocation of one of these methods will return immediately unless the Selector.selectNow() method is invoked in the meantime. In any case the value returned by that invocation may be non-zero. Subsequent invocations of the Selector.select() or Selector.select(long) methods will block as usual unless this method is invoked again in the meantime.

Invoking this method more than once between two successive selection operations has the same effect as invoking it just once.

This selector



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.