import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.Pipe;
import java.nio.channels.spi.SelectorProvider;

import org.apache.commons.io.IOUtils;

public class Test
{
    public static void main(String[] x) throws Exception
    {
        for (int i = 0; i < 200; i++)
        {
            SelectorProvider selectorProvider = SelectorProvider.provider();
            final Pipe pipe = selectorProvider.openPipe();
            final InputStream is = Channels.newInputStream(pipe.source());
            final OutputStream os = Channels.newOutputStream(pipe.sink());
            final int index = i + 1;

            new Thread(() ->
            {
                try
                {
                    try
                    {
                        Thread.sleep(200);
                    }
                    catch (InterruptedException aE)
                    {
                        throw new RuntimeException(aE);
                    }
                    try (
                            InputStream inputStream = new BufferedInputStream(new FileInputStream("C:/a_file_with_33kb_content.txt"));
                            os)
                    {
                        IOUtils.copy(inputStream, os);
                    }
                    System.out.println("WRITTEN for iteration " + index);
                }
                catch (IOException aE)
                {
                    throw new RuntimeException(aE);
                }
            }).start();

            try (is)
            {
                final String output = new String(is.readAllBytes());
                System.out.println("READ for iteration" + index);
            }
        }
    }
} 