NIO十二-Pipe

Java NIO Pipe

A Java NIO Pipe is a one-way data connection between two threads. (線程間的數(shù)據(jù)交換-connection)

A Pipe has a source channel and a sink(下沉、水槽) channel. You write data to the sink channel. This data can then be read from the source channel.

Here is an illustration of the Pipe principle:

pipe-internals.png

? Java NIO: Pipe Internals

Creating a Pipe

You open a Pipe by calling the Pipe.open() method. Here is how that looks:

Pipe pipe = Pipe.open();

Writing to a Pipe

To write to a Pipe you need to access the sink channel. Here is how that is done:

Pipe.SinkChannel sinkChannel = pipe.sink();

You write to a SinkChannel by calling it's write() method, like this:

String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {
    sinkChannel.write(buf);
}

Reading from a Pipe

To read from a Pipe you need to access the source channel. Here is how that is done:

Pipe.SourceChannel sourceChannel = pipe.source();

To read from the source channel you call its read() method like this:

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buf);

The int returned by the read() method tells how many bytes were read into the buffer.

  public static void main(String[] args) throws IOException {

        final Pipe pipe = Pipe.open();

        new Thread(new Runnable() {
            public void run() {
                // sinkChannel 用于寫(xiě)入
                Pipe.SinkChannel inChannel = pipe.sink();

                ByteBuffer byteBuffer = ByteBuffer.allocate(48);
                byteBuffer.put("Test Pipe".getBytes());

                byteBuffer.flip();

                int i = 0;
                try {
                    i = inChannel.write(byteBuffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byteBuffer.clear();
                System.out.println("Thread: " + Thread.currentThread().getName() + ",Write to SinkChannel size:" + i);
            }
        }).start();


        // sourceChannel用于讀取
        new Thread(new Runnable() {
            public void run() {
                Pipe.SourceChannel outChannel = pipe.source();
                ByteBuffer readBuf = ByteBuffer.allocate(48);
                try {
                    outChannel.read(readBuf);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                readBuf.flip();
                System.out.print("Thread: " + Thread.currentThread().getName() + ",Read From SourceChannel:");
                while (readBuf.hasRemaining()) {
                    System.out.print((char) readBuf.get());
                }
            }
        }).start();

    }

如果是兩個(gè)線程,就只能把pipe作為共享變量了。

注意是兩個(gè)線程,而不是兩個(gè)進(jìn)程。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容