Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java- how to wait constantly for an event to happen

Tags:

java

I am creating an application in which there are multiple stages- for each stage a message is sent to my queue in Amazon Simple Work Flow (SWF)... The app should start a new thread for each message that is received.

How do I implement the waiting part- so that the application constantly scans the queue for new messages and takes action the moment a message is received?

like image 975
Arvind Avatar asked Oct 07 '22 15:10

Arvind


1 Answers

BlockingQueue and its implementation LinkedBlockingQueue can be useful here.

When Thread will want to take something from queue by calling

queue.take() 

and queue will be empty, such thread will wait until other thread will put something into queue by calling

queue.put(something). 

Also if queue is full, queue.put() will make putting thread wait until there will be room in queue for new elements.

like image 149
Pshemo Avatar answered Oct 10 '22 05:10

Pshemo