tailieunhanh - Introduction to Programming Using Java Version 6.0 phần 7

Hàng đợi thường được sử dụng trong một máy tính (như trong cuộc sống thực) khi chỉ có một mục có thể được xử lý tại một thời điểm, nhưng một số mặt hàng có thể chờ đợi để chế biến. Ví dụ: • Trong một chương trình Java có nhiều chủ đề, các chủ đề mà muốn | CHAPTER 9. LINKED DATA STRUCTURES AND RECURSION 443 tail newTail Remove and return the front item in the queue. Throws an IllegalStateException if the queue is empty. public int dequeue if head null throw new IllegalStateException Can t dequeue from an empty queue. int firstItem head The previous second item is now first. if head null The queue has become empty. The Node that was deleted was the tail as well as the head of the list so now there is no tail. Actually the class would work fine without this step. tail null return firstItem Return true if the queue is empty. boolean isEmpty return head null end class QueueOfInts Queues are typically used in a computer as in real life when only one item can be processed at a time but several items can be waiting for processing. For example In a Java program that has multiple threads the threads that want processing time on the CPU are kept in a queue. When a new thread is started it is added to the back of the queue. A thread is removed from the front of the queue given some processing time and then if it has not terminated is sent to the back of the queue to wait for another turn. Events such as keystrokes and mouse clicks are stored in a queue called the event queue . A program removes events from the event queue and processes them. It s possible for several more events to occur while one event is being processed but since the events are stored in a queue they will always be processed in the order in which they occurred. A web server is a program that receives requests from web browsers for pages. It is easy for new requests to arrive while the web server is still fulfilling a previous request. Requests that arrive while the web server is busy are placed into a queue to await processing. Using a queue ensures that requests will be processed in the order in which they were received. Queues are said to implement a FIFO policy First In First Out. Or as it is more commonly expressed first come first .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.