Home > Java > Java Pipes vs. Sockets in a Single JVM

Java Pipes vs. Sockets in a Single JVM

In a recent project I should have implemented an InputStream in a java code. There are many ways to achieve this and in this blog post I will show off the performance of just two of them, Sockets and Pipes ( PipedInputStream and PipedOutputStream ).

Scenario is really simple, I’ll send both 10 and 100 million messages from producer to consumer for each scenario. Message is just a sentence, “This is a message sent from Producer”.

I run all the scenario for 3 times and then get the average due to some os environment factor, other processes etc. And also I used the PrintStream as data writer for both test and BufferedReader for reader.

Buffer values can be set for Pipes by passing the long value to the PipedInputStream constructor. And for socket, buffer can be set from BufferedReader constructor. Buffer value for this test is 1024*1024*8 for both scenarios.


Pipe Socket
10M msg 12sec 26sec
100M msg 72sec 239sec

The main code of the test:

public class PipeAndSocketTester {

	public static void main(String args[]) throws IOException {
		boolean autoFlush = false;
		long messageNumber = 10000000;

		PipedOutputStream pos = new PipedOutputStream();
		PipedInputStream pis = new PipedInputStream(pos, 1024*1024*10);

		new PipeWriter(pos,messageNumber,autoFlush).start();
		new PipeReader(pis).start();

		// after above code executed I remove the pipe code then execute follows
		new SocketWriter(4544,messageNumber).start();
		new SocketReader(4544,1024*1024*10).start();

Categories: Java Tags: ,
  1. Doruk Deniz Kutukculer
    May 15, 2013 at 9:49 am

    Hi Fuat,
    I wanted to mention that Pipes are not really a substitute for Sockets since Pipes are used only for communication within OS, between processes. However Sockets can be used for communicating over networks in addition to inter-process communication within OS.

    Thx for the numbers though.

    • Ahmet Fuat Sungur
      May 15, 2013 at 10:02 am

      Thanks for comment Deniz,

      You are right java pipes not for suitable for different jvm processes ( unlike unix pipes ). If we look at the code PipedInputStream constructor needs PipedOutputStream object and so they need to be in same jvm. For my requirement there is just one jvm and in this post I would like to show off the performance comparison between socket and java pipes in a SINGLE JVM. I should change the topics of this post to prevent misunderstanding.

      BTW I am using an external library that needs to InputStream implementation to process incoming data so I should choose one of them. There are additional methods to communicate between java threads like queues but they are not suitable for me.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: