Threads
Application
Order Queue application
Overview of the Order Queue application
- This application simulates a multithreaded ordering application in which multiple order takers, each running the application in a separate thread, generate orders that are added to a queue that runs in the application's main thread.
- The orders are handled by multiple order-handling threads, which remove orders from the queue and display them on the console.
- Each OrderTaker thread creates orders at one-second intervals. The OrderHandler threads retrieve orders at two-second intervals.
- This application creates three OrderTaker threads, each of which creates three orders before ending, and two OrderHandler threads.
- This application is an example of a common design pattern called producer/consumer, in which threads that produce objects place them in a queue so the objects can later be retrieved by threads that consume (process) them.
Console output from the Order Queue application
Starting the order queue.
Starting 3 order taker threads, each producing 3 orders.
Starting 2 order handler threads.
OrderTaker threads OrderHandler threads
============================ =============================
Order #1 created by Thread-1
Order #2 created by Thread-2
Order #1 processed by Thread-3
Order #2 processed by Thread-4
Order #3 created by Thread-0
Order #4 created by Thread-1
Order #5 created by Thread-2
Order #6 created by Thread-0
Order #3 processed by Thread-3
Order #4 processed by Thread-4
Order #7 created by Thread-1
Order #8 created by Thread-2
Order #9 created by Thread-0
Order #5 processed by Thread-3
Order #6 processed by Thread-4
Order #7 processed by Thread-3
Order #8 processed by Thread-4
Order #9 processed by Thread-3