Queues
A queue represents a first-in, first-out (FIFO) collection. The classic analogy is to a line (or queue if you are British) at a ticket window. The first person in line ought to be the first person to come off the line to buy a ticket.
A queue is a good collection to use when you are managing a limited resource. For example, you might want to send messages to a resource that can only handle one message at a time. You would then create a message queue so that you can say to your clients: "Your message is important to us. Messages are handled in the order in which they are received."
The Queue
class has a number of member methods and properties, as shown in Table 17-3.
Table 17-3. Queue methods and properties
|
Method or property |
Purpose |
|---|---|
|
|
Public property that gets the number of elements in the |
|
|
Removes all objects from the |
|
|
Determines if an element is in the |
|
|
Copies the |
|
|
Removes and returns the object at the beginning of the |
|
|
Adds an object to the end of the |
|
|
Returns an enumerator for the |
|
|
Returns the object at the beginning of the |
|
|
Copies the elements to a new array |
Add elements to your queue with the Enqueue command and take them off the queue with Dequeue. You can also see what is next in the queue (without removing it) using Peek. Example 17-6 illustrates.
Example 17-6. Working ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access