MPs queue stream and MPs queue

Source: Internet
Author: User

MPs queue stream and MPs queue

Link: http://www.bdqn.cn/news/201303/8270.shtml

 

A pipeline stream can transmit binary data between two threads.

A pipe stream is like a pipe where data is input at one end and data is output at another end. Usually two different threads are used to control them.

The usage is as follows:

[Html]View plaincopy
  1. Import java. io. IOException;

  2. Import java. io. PipedInputStream;

  3. Import java. io. PipedOutputStream;

  4.  

  5. Public class PipedInputStreamTest {

  6.  

  7. Public static void main (String [] args ){

  8. // MPs queue output stream

  9. PipedOutputStream out = new PipedOutputStream ();

  10. // Pipeline input stream

  11. PipedInputStream in = null;

  12. Try {

  13. // Connect two pipeline streams. You can also call the connect (Piped...) method.

  14. In = new PipedInputStream (out );

  15. Thread read = new Thread (new Read (in ));

  16. Thread write = new Thread (new Write (out ));

  17. // Start the thread

  18. Read. start ();

  19. Write. start ();

  20. } Catch (IOException e ){

  21. E. printStackTrace ();

  22. }

  23. }

  24. }

  25.  

  26. Class Write implements Runnable {

  27. PipedOutputStream pos = null;

  28.  

  29. Public Write (PipedOutputStream pos ){

  30. This. pos = pos;

  31. }

  32.  

  33. Public void run (){

  34. Try {

  35. System. out. println ("the program will write data in 3 seconds. Please wait... ");

  36. Thread. sleep (3000 );

  37. Pos. write ("wangzhihong". getBytes ());

  38. Pos. flush ();

  39. } Catch (IOException e ){

  40. E. printStackTrace ();

  41. } Catch (InterruptedException e ){

  42. E. printStackTrace ();

  43. } Finally {

  44. Try {

  45. If (pos! = Null ){

  46. Pos. close ();

  47. }

  48. } Catch (IOException e ){

  49. E. printStackTrace ();

  50. }

  51. }

  52. }

  53. }

  54.  

  55. Class Read implements Runnable {

  56. PipedInputStream pis = null;

  57.  

  58. Public Read (PipedInputStream pis ){

  59. This. pis = pis;

  60. }

  61.  

  62. Public void run (){

  63. Byte [] buf = new byte [1, 1024];

  64. Try {

  65. Pis. read (buf );

  66. System. out. println (new String (buf ));

  67. } Catch (IOException e ){

  68. E. printStackTrace ();

  69. } Finally {

  70. Try {

  71. If (pis! = Null ){

  72. Pis. close ();

  73. }

  74. } Catch (IOException e ){

  75. E. printStackTrace ();

  76. }

  77. }

  78. }

  79. }

  80.  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.