I am looking for an optimal producer-consumer implementation in Java with the following aspects:Producer reads from server1 socket, and writes to a shared buffer, as well as another server 2 socket:Server1 -> Server2 + shared bufferConsumer reads from a shared buffer, and writes to server3 socket:Shared Buffer -> Server3Writes to shared buffer from Server 1 should be non-blocking (i.e. if the buffer is full it should not block the communication with Server 2). I have considered the following options:Java NIO PipeConcurrentLinkedQueue<Byte> queuePipedInputStream and PipedOutputStreamI assume buffer can have a pre-allocated memory to avoid dynamic memory allocation overhead? Or should that not be a concern? If so what would be a fast implementation.What would be the fastest?I have already tried out PipedInputStream and PipedOutputStream. My overhead compared to just forwarding data from server1->server2 was more than 12% this seemed excessive, which is why I came up with more options.The specific part of the code already implemented, which is in the critical region (pertains to performance) is as follows in CommunicateToServerAndPipe: public void run() {        try {            String line = null;            final byte[] request = new byte[1024];            int bytesRead;            //read from server 1 socket            while (isRunning && (bytesRead = in.read(request)) != -1) {                //write to server 2 socket                out.write(request, 0, bytesRead);                out.flush();                //write to shared buffer pipe                this.register.pos.write(request, 0, bytesRead);            }     register.kill();    } catch (IOException ex) {        if(ex instanceof SocketTimeoutException) {            // The socket simply timed out. Kill, then exit.            register.kill();            return;        }        this.kill();    }}I have made my initial code available here.
Optimal Implementation Choice for Producer-Consumer in Java
java;performance;producer consumer