Can you give an example where the Queue data structure can be specially helpful
views:
317answers:
5All of these questions are easily googled. Do your homework on your own.
In the case that you need to process requests in the order that they are received you would use a Queue. Or a FIFO type structure...first in first out...which preserves the request order. To take it further you could use an offline queue similar to MSMQ so disconnect the request from the actual request taker. Say you have a website that needs to send emails. This is a perfect place for a queue where the website stashes the request in the queue for an offline processor so that the web site is not directly tied to the SMTP server that is needed for processing the mail. This then frees up the worker thread on the web site to process web requests.
queue has many uses in algorithms such as breadth first search. and that is very useful.
Queues are most commonly used for scheduling and request handling applications.
For example, anything where you have one process creating requests and another process handling the requests you would use a queue to hold the requests.
Normally a queue is in FIFO order - requests are processed in the order they are received, but they can also be ordered in other ways (a priority queue for example).
A few examples:
An operating system scheduling threads for execution on a CPU would use a queue to determine which thread is next to execute.
A web server handling HTTP requests would use a queue to pass requests on to threads that will actually server up the web pages.
A printer handling requests for print jobs.
In some cases, items can be (safely) pushed onto and pulled off of queue structures by multiple threads. For example, say you want only 5 simultaneous connections to a credit card processor. 5 threads can be started and checking (waiting) for items to appear on the queue. Requests to process credit cards are placed into that queue structure by other threads would be processed as soon as possible. (That's oversimplified. There are many other considerations, like waiting for a response, thread safety, concurrency, locking, mood of the developer, phase of the moon...)