• Post Reply Bookmark Topic Watch Topic
  • New Topic

Producer Consumer without interruption of production  RSS feed

 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Java Threads (book ordered, delivered soon) are new to me. I am facing a situation of producer consumer.
In my case the production is not going to wait for consumers. So, consumers will not consume all production...
I think if we take this consumer producer example from the Java Tutorials
then by simply modifying the put method of Drop class the production is continuous.



My question: is there any standard library class to achieve the same result instead of the Drop class?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but look for Blocking Queues.
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Not sure, but look for Blocking Queues.

Thank you very much. What I want is equivalent to:
put() => offer(e, 0 , TimeUnit.MILLISECONDS)
take() => poll(Integer.MAX_VALUE, TimeUnit.SECONDS) or simply take()...
Cheers!
 
Tim Cooke
Marshal
Posts: 3652
184
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a standard library to do what you're proposing, as it's quite bespoke. As a 'standard' solution you'd be more likely to find a bounded queue that will simply refuse new messages from a producer when the queue is full. In fact, a bounded queue of size 1 will be very close to your solution apart from the queue refusing new messages, where your solution constantly overwrites the single message item.
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim for your reply. Well, the solution offer(e,0,...)/take() behaves as expected as described in the Java docs. This is I think what my custom Drop achieves too.
 
Tim Cooke
Marshal
Posts: 3652
184
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What JavaDocs? Your code doesn't have any JavaDocs.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at RxJava:

https://github.com/ReactiveX/RxJava/wiki

Hope that fits your needs!
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:What JavaDocs? Your code doesn't have any JavaDocs.


From BlockingQueue one can read:

BlockingQueue methods come in four forms, with different ways of handling operations that cannot be satisfied immediately, but may be satisfied at some point in the future: one throws an exception, the second returns a special value (either null or false, depending on the operation), the third blocks the current thread indefinitely until the operation can succeed, and the fourth blocks for only a given maximum time limit before giving up. These methods are summarized in the following table:
Throws exception> Special value Blocks Times out
Insert add(e) offer(e) put(e) offer(e, time, unit)
Remove remove() poll() take() poll(time, unit)
Examine element() peek() not applicable not applicable

 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Have a look at RxJava:

https://github.com/ReactiveX/RxJava/wiki

Hope that fits your needs!


Thanks! The standard Library fits my needs.
 
Greg Pata
Ranch Hand
Posts: 31
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, in the Java Tutorials one can replace in the Producer class

by

to avoid the interruption of production by the consumer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!