tag:blogger.com,1999:blog-8712770457197348465.post3916552526419439244..comments2024-03-29T00:43:22.798-07:00Comments on Javarevisited: How to use SynchronousQueue in Java? Prouder Consumer Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-8712770457197348465.post-64006674074142649682017-09-19T20:36:09.437-07:002017-09-19T20:36:09.437-07:00Hi, is it possible to have multiple producers and ...Hi, is it possible to have multiple producers and one consumer per SyncronousBlockingQueue or does it work only for one producer and consumer?Charvi's Bloghttps://www.blogger.com/profile/04597318833125742172noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56364445885806083332016-06-23T03:16:54.418-07:002016-06-23T03:16:54.418-07:00Very helpful article. Thanks a tonn.Very helpful article. Thanks a tonn.Anonymoushttps://www.blogger.com/profile/05160995246146544078noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13683565775747274082016-06-10T23:32:08.215-07:002016-06-10T23:32:08.215-07:00@Sunny: When a thread put the element on Synchrono...@Sunny: When a thread put the element on Synchronous Queue, internally that thread goes to wait state on SynchronousQueue Object and when another thread take the element from the queue, it internally calls notify to <a href="http://singletonjava.blogspot.com/2016/02/java-synchronousqueue-synchronous-queue.html" rel="nofollow"> Java SynchronousQueue</a><br />Object leading to one waiting thread Guptahttp://singletonjava.blogspot.com/2016/02/java-synchronousqueue-synchronous-queue.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49104452636165870172016-05-02T08:10:46.570-07:002016-05-02T08:10:46.570-07:00How are these two threads not leading to deadlock?...How are these two threads not leading to deadlock? I mean there's nothing to put and remove from the SynchronousQueue as it has 0 capacity. Shouldn't it lead to a deadlock ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63479520031920766962014-10-09T06:16:12.726-07:002014-10-09T06:16:12.726-07:00Hello Gaurav, you can develop that logic based upo...Hello Gaurav, you can develop that logic based upon type of message, or content of message if no two message is related to other, otherwise you need to ensure that some messages are processed by one particular thread and should be put in one queue. For example in Electronic Trading System, we usually process all messages for one symbol in one queue to avoid processing Cancel/Modification request javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29340968182672924402014-10-09T01:18:02.305-07:002014-10-09T01:18:02.305-07:00Thanks Javin. That's really a good suggestion....Thanks Javin. That's really a good suggestion. I could try the two tier approach. I think i need to develop some sort of fairness logic for the first consumer who distributes the messages to different queues such that second tier queues have fair distribution of messages.<br />Also Can you throw some light on how to do deal with the clean up operation in case of producer has died. I guess I Gauravhttps://www.blogger.com/profile/01514578422510760390noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82125223236591114822014-10-08T08:40:34.788-07:002014-10-08T08:40:34.788-07:00Given your producer is very fast, there is a risk ...Given your producer is very fast, there is a risk of OutOfMemoryException in case of using blocking queue if consumer happened to be slow. I would suggest using a 2 tier approach where producer insert in a BlockingQueue and one Consumer takes and distribute messages into different queue, then you can use thread pool executor to assign each thread their own bounded queue. The intermediate thread javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2392785829936503872014-10-08T07:43:17.160-07:002014-10-08T07:43:17.160-07:00Thanks Javin for reply. I am using the Synchronous...Thanks Javin for reply. I am using the Synchronous Queue and by testing I realised only one consumer is able to consume the message. So that's what I was looking for.<br />Earlier I thought of having separate queue for each consumer, however that will defeat the purpose I want to achieve. Producer in my case is producing about million message hour and I want to process those message in Gauravhttps://www.blogger.com/profile/01514578422510760390noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45101801411180527562014-10-08T07:04:16.526-07:002014-10-08T07:04:16.526-07:00@Gaurav by removing that element from Queue, but i...@Gaurav by removing that element from Queue, but in case of multiple consumer having separate queue for each consumer is better design because it will reduce contention and prevent performance penalty due to locking and synchronization.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87106937688732441012014-10-08T01:22:22.279-07:002014-10-08T01:22:22.279-07:00First of all very nice article.
I have a query on ...First of all very nice article.<br />I have a query on this. In case of one producer and multiple consumer scenario, how can i ensure that only one of the consumer consumes the message and not all of them.<br />ThanksGauravhttps://www.blogger.com/profile/01514578422510760390noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-92018317798909589252014-06-09T19:28:12.663-07:002014-06-09T19:28:12.663-07:00Excellent. You explain things the best understanda...Excellent. You explain things the best understandable way. Code is too neat. <br />Thankskewldudehttps://www.blogger.com/profile/09707226816781984078noreply@blogger.com