I hava a java application that uses Buffers to pass object around between threads. Generally I synchronize these buffers using BufferUtils. If I have just one thread that adds objects to a buffer, and only adds, and just one other thread that removes objects from the buffer, and only removes; does the buffer between them need to be synchronized?
I have a situation now where the two threads in question are instantiated from spring beans and can only get their hands on the shared buffer by having a third bean for the buffer that they both reference. I couldn’t figure out how to make a synchronized buffer from a bean, hence the question regarding whether or not it was necessary.
Perhaps you could use a BlockingQueue to pass those objects around. And the blocking queue is thread safe. Plus it offers several put/get variants that can be used nicely.
The method put, for instance, will make the thread block until there is enough room in the queue.
Offer method, can be used with a timeout, so if it doesn’t manage to add to the queue it will return false.
You could check it out
BlockingQueue