I want to implement a queue, that is hit by multiple threads.
This is stack is in a singleton class.
Now, a simple solution is to synchronize this? I assume it would need this as standard?
However, I want to prioritize writing to it.
So, write is high priority, read is low priority.
Is this possible?
Ideally writing by multiple threads without synchronizing would be great, if possible.
Why do you want to avoid synchronizing? It’s possible to write “lock-free” structures, but it’s quite tricky and easy to get wrong.
If I were you, I’d use
ArrayBlockingQueueorConcurrentLinkedQueue(or one of the other structures fromjava.util.concurrent) and make your life easy!Oh, and I missed the bit about prioritising reads over writes. You can do that with the
ReentrantReadWriteLockclass. Then you don’t need a thread-safe queue – you just lock externally using the read-write lock depending on whether you’re reading or writing.