0

What I need:
I want to block a thread until I have (or I haven't) some certain object in the queue.

What I have:
The solution I made isn't very nice. It's just a loop with the sleep(1000) which searches if the object is gone from the queue or not. It isn't very nice because it spends time to sleep and spends time to search a certain object in the queue. Also I wanted to not search a queue but to see a count of references of object (if it is 2 then your queue still points on that object) but I afraid of this solution and also don't know how to realize it.

The full task:
I am writing an application which sends messages to the RabbitMQ nodes and confirms them. Before message is sent I save a message ID in the synchronized queue (because several threads send messages). When the message is confirmed my application receives an ACK with the message ID which was confirmed. The client thread (which send messages) has to be blocked and wait till he didn't receive a confirmation of the message he sent.

Is it possible to implement such a solution? Doesn't meter if we have to put an object or remove an object from the queue. Both solutions will be fine.

Oleksandr
  • 3,574
  • 8
  • 41
  • 78
  • It might be better to implement a `removeFromQueue` method that notifies your client thread if the object it is waiting for is removed. Try to move some work away from the client thread. – Luke Joshua Park Oct 05 '16 at 20:04
  • If I were to rephrase it, you have a method which sends the message and needs to receive and acknowledgement before the next thread accesses it. Making the method (one that sends the request) synchronized should solve the problem if this is the case. – HARDI Oct 05 '16 at 20:08
  • Why do we need synchronized method which sends messages? I am using an ArrayBlockingList to get the channel which I need and then I send a message and free that channel immediately for other threads. – Oleksandr Oct 05 '16 at 20:15

0 Answers0