// Resume interest in OP_READ key.interestOps (key.interestOps() |
// Resume interest in OP_READ key.interestOps (key.interestOps() | SelectionKey.OP_READ); // Cycle the selector so this key is active again key.selector().wakeup(); } } } Because the thread doing the selection will loop back and call select() again almost immediately, the interest set in the key is modified to remove interest in read-readiness. This prevents the selector from repeatedly invoking readDataFromSocket() (because the channel will remain ready to read until the worker thread can drain the data from it). When a worker thread has finished servicing the channel, it will again update the key’s interest set to reassert an interest in read-readiness. It also does an explicit wakeup() on the selector. If the main thread is blocked in select(), this causes it to resume. The selection loop will then cycle (possibly doing nothing) and reenter select() with the updated key. 4.6 Summary In this chapter, we covered the most powerful aspect of NIO. Readiness selection is essential to large-scale, high-volume server-side applications. The addition of this capability to the Java platform means that enterprise-class Java applications can now slug it out toe-to-toe with comparable applications written in any language. The key concepts covered in this chapter were: Selector classes The Selector, SelectableChannel, and SelectionKey classes form the triumvirate that makes readiness selection possible on the Java platform. In Section 4.1, we saw how these classes relate to each other and what they represent. Selection keys In Section 4.2, we learned more about selection keys and how they are used. The SelectionKey class encapsulates the relationship between a SelectableChannel object and a Selector with which it’s registered. Selectors Selection requests that the operating system determine which channels registered with a given selector are ready for I/O operation(s) of interest. We learned about the selection process in Section 4.3 and how to manage the key set returned from a call to select(). We also discussed some of the concurrency issues relating to selection. 158
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp hosting services