• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Inter thread communication - Using PipedStreams Vs ConcurrentQueus

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I a Java Newbi,

am playing around with Threads for some time.

Right now I am trying to come with a small app where in there will be multiple Producers generating JavaObjects and a single consumer reading these Objects.

example.
2 Producers generating objects and pushing them into a Pipe or Queue
1 Consumer reading the produced object from this Pipe or Queue.

For this which would be good. Should I go for PipedStreams or a Concurrent Queue??

I need my java app to have a smaller memory foot print & run faster.

which of the two would be good ? please give me some explainations or some pointers ( advantages & disadvantages)

Thanks in advance,
Omganesh
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a lot of differences between the two.

For starters, what kind of data do you want to transport? Complex objects will be easier to transport between threads using a Queue, rather than a Pipe. With the Pipes you would have to serialize the objects or transform them in some way to get them through the Stream, then reverse the procedure on the receiving side. With Queues you just put the object in the Queue and pull it out on the consumer.

Another: A PipedInputStream (consumer) can only connect to a single PipedOutputStream (producer), and it wouldn't be safe to share the PipedOutputStream with multiple threads without further synchronization. So on the consumer you would need 2 PipedInputStreams, one connecting to each producer's PipedOutputStream. In your situation of 2 producers -> 1 consumer, a thread safe Queue implementation will be easier. Both producers and the consumer can use the same Queue. But that may not be exactly what you want.

A Queue may be easier for the general purpose but if you have pre-written code optimized around stream communication, then perhaps the Pipe connection would be easier to implement - specifically if the data is all primitives or Strings (where PipedReader/Writer could be used).

Which will be faster is hard to predict. If you can get both working safely the only way to make the speed comparison would be to test. My guess is that the performance factor will be not be worth comparing though. Either using Streams to communicate or using Queues to collate data will be the better design decision.
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also another issue with piped streams -- apparently, the stream keeps track of the last reader and last writer thread. If you write to a pipe and the last reader thread is terminated, you will get a broken pipe exception. And you will continue to get this exception til another reader reads from the pipe.

Its actually a bit silly, but something to look out for, nonetheless.

Henry
 
om ganesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry & Steve for the response..

Steve's question
what kind of data do you want to transport?
I am reading Objects from a data source and then passing them onto the Queue / Pipe.

I would now try out the ConcurrentLinkedQueue & see how it goes.


was playing around ... with the PipedStream..

Faced the Scenario which Henry had mentioned .

2 Producer threads are using the same PipedOutputStream reference but the pipe is broken if any one of the Producer terminates before the other

So the Producer thread which finished first would have to be running until the Producer 2 thread too has finished.

One more question.

If i go ahead with the PipedOutputStream, can data over 1MB be stored in it until it is read by the Consumer ?
 
om ganesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just tried with ConcurrentLinkedQueue,

the sample program ran smooth.. without much ...

here is the code .


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic