编程小贴士

给你的编程提供小点子


Java IO: 管道

Java IO中的管道为运行在同一个JVM中的两个线程提供了通信的能力。所以管道也可以作为数据源以及目标媒介。

你不能利用管道与不同的JVM中的线程通信(不同的进程)。在概念上,Java的管道不同于Unix/Linux系统中的管道。在Unix/Linux中,运行在不同地址空间的两个进程可以通过管道通信。在Java中,通信的双方应该是运行在同一进程中的不同线程。

 

通过Java IO创建管道

可以通过Java IO中的PipedOutputStream和PipedInputStream创建管道。一个PipedInputStream流应该和一个 PipedOutputStream流相关联。一个线程通过PipedOutputStream写入的数据可以被另一个线程通过相关联的 PipedInputStream读取出来。

Java IO管道示例

这是一个如何将PipedInputStream和PipedOutputStream关联起来的简单例子:

tips1000_2014-10-24_00-28-50

你也可以使用两个管道共有的connect()方法使之相关联。PipedInputStream和PipedOutputStream都拥有一个可以互相关联的connect()方法。

管道和线程

请记得,当使用两个相关联的管道流时,务必将它们分配给不同的线程。read()方法和write()方法调用时会导致流阻塞,这意味着如果你尝试在一个线程中同时进行读和写,可能会导致线程死锁。

管道的替代

除了管道之外,一个JVM中不同线程之间还有许多通信的方式。实际上,线程在大多数情况下会传递完整的对象信息而非原始的字节数据。但是,如果你需要在线程之间传递字节数据,Java IO的管道是一个不错的选择。

77 Responses to “ Java IO: 管道 ”

  1. me suba说道:

    Il1iGx I will bookmark your blog and check once more right here regularly.

  2. This is one awesome blog.Really thank you! Great.

  3. Some truly prime articles on this web site , bookmarked.

  4. tractors说道:

    There may be noticeably a bundle to learn about this. I assume you made certain nice factors in features also.

  5. drawing说道:

    Why viewers still use to read news papers when in this technological globe everything is accessible on web?

  6. It as exhausting to search out educated people on this matter, but you sound like you know what you are speaking about! Thanks

  7. dau tram be tho说道:

    Look complicated to far added agreeable from you! By the way, how

  8. This is one awesome article.Really thank you! Cool.

  9. Thanks for sharing, this is a fantastic article post.Really thank you! Really Cool.

  10. Trap an hoi说道:

    Wonderful beat ! I would like to apprentice while you amend

  11. trap an hoi dep说道:

    Woh I like your blog posts, saved to favorites !.

  12. This blog has lots of very useful stuff on it. Thanks for sharing it with me!

  13. Nice weblog here! Also your site quite a bit up very fast! What web host are you the usage of? Can I am getting your affiliate link to your host? I desire my web site loaded up as quickly as yours lol

  14. status online说道:

    This is a beautiful picture with very good light

  15. The Lost Ways说道:

    Where I come from we don at get much of this sort of writing. Got to look around all over the internet for such relevant pieces. I congratulate your effort. Keep it up!

  16. wow, awesome blog.Much thanks again. Will read on…

  17. Asuquo说道:

    Very good article post.Thanks Again. Much obliged.

  18. du an Rio Verde说道:

    Muchos Gracias for your article. Awesome.

  19. vong nguyet que说道:

    web owners and bloggers made good content as you did, the

  20. vong nguyet que说道:

    share. I know this is off topic but I simply needed to

  21. This awesome blog is obviously awesome as well as diverting. I have chosen helluva helpful tips out of it. I ad love to return every once in a while. Thanks!

  22. trash cans说道:

    Thanks-a-mundo for the blog.Really thank you! Cool.

  23. mp4 download说道:

    Network Marketing is not surprisingly very popular because it can earn you numerous revenue within a really brief time period..

  24. Really excellent information can be found on web blog.

  25. sales tool说道:

    What as up, just wanted to mention, I enjoyed this post. It was funny. Keep on posting!

  26. Spot on with this write-up, I absolutely believe that this web site needs far more attention. I all probably be returning to see more, thanks for the info!

  27. Yes. It should get the job done. If it doesn at send us an email.

  28. online loans说道:

    I reckon something genuinely special in this site.

  29. sim viettel说道:

    Thanks-a-mundo for the blog post. Really Great.

  30. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Actually Fantastic. I am also an expert in this topic therefore I can understand your effort.

  31. sidekick说道:

    You, my friend, ROCK! I found just the info I already searched everywhere and just couldn at locate it. What an ideal web-site.

  32. useful说道:

    I really liked your blog post.Thanks Again. Really Cool.

  33. beauty说道:

    Really appreciate you sharing this article post.Much thanks again. Much obliged.

  34. The Silent Shard This will likely almost certainly be quite handy for some of your respective positions I decide to you should not only with my website but

  35. wholesale fabric designer apparel fabric emerges to you personally together with lowest price.

  36. Hosting说道:

    This is really interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your fantastic post. Also, I ave shared your website in my social networks!

  37. Polainas tejidas说道:

    You ave made some good points there. I checked on the internet to find out more about the issue and found most people will go along with your views on this website.

  38. Tejidos说道:

    Say, you got a nice blog article.Much thanks again. Want more.

  39. blue nike说道:

    user in his/her brain that how a user can understand it.

  40. Thanks-a-mundo for the article post.Really looking forward to read more. Will read on

  41. Really appreciate you sharing this article.Thanks Again. Keep writing.

  42. Waec说道:

    Thanks for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next post thank you once again.

  43. ps4 ark servers说道:

    that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often

  44. KENJI说道:

    Wow, great post.Much thanks again. Fantastic.

  45. It as hard to find experienced people for this subject, but you sound like you know what you are talking about! Thanks

  46. Moskito说道:

    What would be your subsequent topic subsequent week in your weblog.*:* a-

  47. Say, you got a nice blog post.Much thanks again. Much obliged.

  48. Apple Reparatur说道:

    I was able to find good information from your articles.

  49. This genuinely answered my predicament, thank you!

  50. Rattling superb info can be found on website.

  51. Your style is unique in comparison to other folks I ave read stuff from. Thanks for posting when you have the opportunity, Guess I all just book mark this site.

  52. tututorials说道:

    Wow! Thank you! I continuously needed to write on my blog something like that. Can I include a portion of your post to my website?

  53. hotels说道:

    Whats up. Very cool site!! Man.. Beautiful.. Superb.. I will bookmark your site and take the feeds alsoI am glad to find so much helpful info here in the post. Thanks for sharing.

  54. Thanks for the blog article.Really thank you! Great.

  55. Merely a smiling visitor here to share the love (:, btw outstanding design.

  56. This page truly has all the info I wanted concerning this subject and didn at know who to ask.

  57. Some genuinely interesting info , well written and broadly user genial.

  58. I truly love your site.. Pleasant colors & theme.

  59. mjagkie okna说道:

    This is one awesome blog.Really thank you! Cool.

  60. devotionals说道:

    You made some clear points there. I looked on the internet for the topic and found most people will agree with your website.

  61. wow, awesome post.Really looking forward to read more. Will read on…

  62. Well I truly liked studying it. This information offered by you is very useful for proper planning.

  63. like this说道:

    Im obliged for the post.Much thanks again. Fantastic.

  64. Wohh precisely what I was searching for, regards for putting up.

  65. This is my first time pay a visit at here and i am truly pleassant to read all at alone place.

  66. see this website说道:

    Thanks for the article post.Thanks Again. Really Cool.

  67. pretty useful material, overall I imagine this is worthy of a bookmark, thanks

  68. classified post说道:

    I truly appreciate this post.Really looking forward to read more.

  69. Thanks for this wonderful post! It has been extremely useful. I wish that you will carry on sharing your knowledge with me.

  70. see more说道:

    Right now it sounds like Expression Engine is the top blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  71. It as not that I want to replicate your web-site, but I really like the layout. Could you tell me which style are you using? Or was it tailor made?

  72. Really enjoyed this article post.Really thank you! Cool.

  73. This is my first time go to see at here and i am really pleassant to read all at alone place.

  74. I really liked your article.Much thanks again. Fantastic.

  75. more info说道:

    Outstanding post but I was wondering if you could write a litte more on this subject? I ad be very grateful if you could elaborate a little bit further. Thank you!

  76. Your current positions continually have much of really up to date info. Where do you come up with this? Just declaring you are very imaginative. Thanks again

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>