All for Joomla All for Webmasters

PipedInputStream and PipedOutputStream classes

The PipedInputStream and PipedOutputStream classes can be used to read and write data simultaneously. Both streams are connected with each other using the connect() method of the PipedOutputStream class.

Example of PipedInputStream and PipedOutputStream classes using threads

Here, we have created two threads t1 and t2. The t1 thread writes the data using the PipedOutputStream object and the t2thread reads the data from that pipe using the PipedInputStream object. Both the piped stream object are connected with each other.

  1. import*;
  2. class PipedWR{
  3. public static void main(String args[])throws Exception{
  4. final PipedOutputStream pout=new PipedOutputStream();
  5. final PipedInputStream pin=new PipedInputStream();
  6. pout.connect(pin);//connecting the streams
  7. //creating one thread t1 which writes the data
  8. Thread t1=new Thread(){
  9. public void run(){
  10. for(int i=65;i<=90;i++){
  11. try{
  12. pout.write(i);
  13. Thread.sleep(1000);
  14. }catch(Exception e){}
  15. }
  16. }
  17. };
  18. //creating another thread t2 which reads the data
  19. Thread t2=new Thread(){
  20. public void run(){
  21. try{
  22. for(int i=65;i<=90;i++)
  23. System.out.println(;
  24. }catch(Exception e){}
  25. }
  26. };
  27. //starting both threads
  28. t1.start();
  29. t2.start();
  30. }}


submit to reddit

Leave a Reply