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 用于寫入
                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作為共享變量了肃廓。

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市坦胶,隨后出現(xiàn)的幾起案子透典,更是在濱河造成了極大的恐慌,老刑警劉巖顿苇,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件峭咒,死亡現(xiàn)場離奇詭異,居然都是意外死亡纪岁,警方通過查閱死者的電腦和手機凑队,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來幔翰,“玉大人漩氨,你說我怎么就攤上這事西壮。” “怎么了叫惊?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵款青,是天一觀的道長。 經(jīng)常有香客問我赋访,道長可都,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任蚓耽,我火速辦了婚禮渠牲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘步悠。我一直安慰自己签杈,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布鼎兽。 她就那樣靜靜地躺著答姥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪谚咬。 梳的紋絲不亂的頭發(fā)上鹦付,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天,我揣著相機與錄音择卦,去河邊找鬼敲长。 笑死,一個胖子當(dāng)著我的面吹牛秉继,可吹牛的內(nèi)容都是我干的祈噪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尚辑,長吁一口氣:“原來是場噩夢啊……” “哼辑鲤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起杠茬,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤月褥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瓢喉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吓坚,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年灯荧,在試婚紗的時候發(fā)現(xiàn)自己被綠了礁击。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖哆窿,靈堂內(nèi)的尸體忽然破棺而出链烈,到底是詐尸還是另有隱情,我是刑警寧澤挚躯,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布强衡,位于F島的核電站,受9級特大地震影響码荔,放射性物質(zhì)發(fā)生泄漏漩勤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一缩搅、第九天 我趴在偏房一處隱蔽的房頂上張望越败。 院中可真熱鬧,春花似錦硼瓣、人聲如沸究飞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽亿傅。三九已至,卻和暖如春瘟栖,著一層夾襖步出監(jiān)牢的瞬間葵擎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工半哟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留酬滤,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓镜沽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親贱田。 傳聞我的和親對象是個殘疾皇子缅茉,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容