美文网首页Nio
NIO十二-Pipe

NIO十二-Pipe

作者: AlanKim | 来源:发表于2019-03-07 08:51 被阅读0次

Java NIO Pipe

A Java NIO Pipe is a one-way data connection between two threads. (线程间的数据交换-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 用于写入
                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();

    }

如果是两个线程,就只能把pipe作为共享变量了。

注意是两个线程,而不是两个进程。

相关文章

  • NIO十二-Pipe

    Java NIO Pipe Creating a Pipe Writing to a Pipe Reading f...

  • Java NIO 教程(十二) Pipe

    参考:http://ifeve.com/pipe/原文地址 目录 Java NIO教程 Java NIO 教程(一...

  • java nio Pipe

    Java NIO Pipe是2个线程之间的单向数据连接,Pipe有一个source通道和一个sink通道。可以将数...

  • Java NIO(五):Pipe

    Pipe Pipe(管道)可用于线程之间的数据传输,它是单向的,它有一个 source 通道和 sink 通道,数...

  • Java NIO Pipe(管道)

    Java NIO 中的管道时两个线程之间的单向数据连接。Pipe有一个Source通道和一个Sink通道。数据会被...

  • Java NIO(8) - 管道

    5.管道(Pipe) Java NIO 管道是2个线程之间的单向数据连接。Pipe有一个source通道和一个si...

  • Pipe

    Java NIO Pipe是两个线程之间的单向数据连接。一个Pipe有一个source channel和一个sin...

  • Java NIO(十三) Pipe(管道)

    Java NIO Pipe是两个线程之间的单向数据连接。 一个管道有一个源通道和一个接收通道。 您将数据写入接收器...

  • Java NIO系列教程(十一) Pipe

    参考文章:http://ifeve.com/pipe/

  • 聊聊storagetapper的pipe

    序 本文主要研究一下storagetapper的pipe Pipe storagetapper/pipe/pipe...

网友评论

    本文标题:NIO十二-Pipe

    本文链接:https://www.haomeiwen.com/subject/dnjaeqtx.html