Reputation: 189
As my title said I have to implement a program where threads communicate with each other by sending message (serializable object). The fact is that I want threads to be blocked if they don't have a message waiting. I have already checked on Google and I have found 2 solutions : the first one use ObjectStream with PipedStream and the second one use BlockingQueue. My problem is that the 2 solutions seem not to be the best one because PipedStream have lots of bug (I have so much while doing tests) and BlockingQueue do not blocked the thread when the queue is empty.
So has everyone a solution for my problem ?
Thanks in advance ;)
Upvotes: 0
Views: 76
Reputation: 952
This is basically a producer/consumer, where a thread can be either or both a producer and a consumer.
I believe that BlockingQueue
offers you the solution you need (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html). Particularly, look at the put()
and take()
methods.
Upvotes: 1
Reputation: 144
The take method of BlockingQueue does wait
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#take()
Upvotes: 1