The Pipe class creates a pair of Channel
public static abstract class SourceChannel extends AbstractSelectableChannel implements ReadableByteChannel, ScatteringByteChannel public static abstract class SinkChannel extends AbstractSelectableChannel implements WritableByteChannel, GatheringByteChannel } Figure 3-11. A pipe is a pair of looped channels An instance of Pipe is created by invoking the Pipe.open() factory method with no arguments. The Pipe class defines two nested channel classes to implement the pipeline. These classes are Pipe.SourceChannel (the read end of the pipe) and Pipe.SinkChannel (the write end of the pipe). These Channel instances are created when the Pipe object is created and can be fetched by calling the source() and sink() methods, respectively, on the Pipe object. At this point, you may be wondering what pipes are useful for. You can’t use Pipe to set up a Unix-like pipe between operating system-level processes (you can use SocketChannel for that). The source and sink channels of Pipe provide functionality similar to java.io.PipedInputStream and java.io.PipedOutputStream but with full channel semantics. Notice that SinkChannel and SourceChannel both extend from AbstractSelectableChannel (and thus SelectableChannel), which means that pipe channels can be used with selectors (see Chapter 4). Pipes can be used only to pass data within the same JVM. There are far more efficient ways of passing data between threads, but the advantage of using pipes is encapsulation. Producer and consumer threads can be written to the common Channel API. The same code can be used to write data to a file, socket, or pipe, depending on the type of channel it’s given. Selectors can be used to check for data availability on pipes just as easily as on socket channels. This might allow a single consumer thread to efficiently collect data from multiple channels, in any combination of network connections or local worker threads, using a single Selector. The implications for scalability, redundancy, and reusability are significant. Another useful application of Pipes is for testing. A unit-testing framework can connect a class to be tested to the write end of a pipe and check the data that comes out the read end. It can also set up the class being tested on the read end of the pipe and write controlled test data to it. Both scenarios can be very useful for regression testing. 123
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services