Tuesday 20 September 2016

Java Tutorial : Java IO (PipedReader and PipedWriter-V3)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=MD6KIEnoN3g&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java IO (PipedReader and PipedWriter-V3) 
PipedReadWriteDemo.java
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PipedReadWriteDemo
{

    final static PipedWriter pw = new PipedWriter();
    final static PipedReader pr = new PipedReader();

    class PipedOutputThread implements Runnable
    {
        @Override
        public void run()
        {
            for (int i = 1; i <= 5; i++)
            {
                try
                {
                    pw.write("Welcome " + i + "\n");
                    Thread.sleep(1000);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            try
            {
                pw.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    class PipedInputThread implements Runnable
    {
        @Override
        public void run()
        {
            try
            {
                int i = 0;
                while ((i = pr.read()) != -1)
                {
                    System.out.print((char) i);
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            try
            {
                pr.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args)
    {
        try
        {
            pw.connect(pr);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        ExecutorService service = Executors.newFixedThreadPool(2);
        PipedReadWriteDemo pipedWriteReadDemo = new PipedReadWriteDemo();
        service.execute(pipedWriteReadDemo.new PipedOutputThread());
        service.execute(pipedWriteReadDemo.new PipedInputThread());
    }
}
Output
Welcome 1
Welcome 2
Welcome 3
Welcome 4
Welcome 5

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_PipedReader_PipedReader_V3_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_PipedReader_PipedReader_V3_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/301d989d72a8b053f20342cfe0bd4a5c143c23aa/BasicJava/JavaIODemo_PipedReader_PipedReader_V3_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment