-3

Im implimenting a java game with multiplayer and i have a sender thread to send a messages from a Queue to another player. I have read here: https://developer.android.com/reference/java/util/concurrent/BlockingQueue.html "A Queue that additionally supports operations that wait for the queue to become non-empty"

which operetions and how to use them , and i know that an infinte loop that always checks if something Queue is what i want to avoid.

tamirz12345
  • 47
  • 2
  • 8

2 Answers2

1

Since there appears to be no out-of-the-box solution to this problem, I had to implement my own.

So, what I do is that my void block() method invokes poll() and saves the item returned. Then, every method that obtains an item from the queue first checks if we have a saved item, and if so, returns it and clears it, otherwise it delegates to the underlying java blocking queue.

Luckily I only consume items from a single thread, so I do not have to worry about race conditions.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

take() will allow you to wait until an element becomes available in the queue or use poll(long timeout, TimeUnit unit) to wait until a specified time.

nullPointer
  • 133
  • 7