A student compares programs that run on a single-threaded event loop with control-driven programs. Assume all arriving events are retained in one first-in, first-out (FIFO) queue, in arrival order. The loop dispatches one event at a time, and its handler runs to completion without interruption before the next event is dispatched. Each event invokes one registered handler, and user-input and display-update events use this same loop. Which statements are true under these assumptions? Select all that apply.
Events that arrive while a handler is running are discarded, so users must repeat any click made during that time.
Registering a handler runs it once immediately, so the program can confirm that the handler works.
A handler that performs a long computation delays every event waiting behind it, so the interface can appear frozen until it returns.
Handlers invoked for distinct queued events begin in the order those events entered the queue, regardless of the order the handler functions were defined in the source code.
A control-driven program cannot respond to user input, because its statements always run in a fixed order.
Unlock full course notes by signing up
Tracing shared state through queued events
A single-threaded program registers three handlers that all update the shared variable total. The object clock emits a tick event at regular intervals.
After setup, these events reach the event queue in this order: tick, click, keydown, mouseover, click, tick. No handler is registered for mouseover. What is the value of total after the queue has emptied? Give your answer as an integer.
Predicting log output from setup code and posted events
The pseudocode below runs on a single-threaded event loop with one FIFO queue. on(name, f) registers handler f for events called name. post(name) only adds an event to the back of the queue and returns immediately. A handler is looked up when its event is dispatched, not when the event is posted. Setup runs to completion before run_event_loop() starts dispatching.
While setup is running, just after the post("tick") line executes, the user clicks the window, so a click event joins the queue behind the tick. No other events occur. Arrange the printed lines in the order they appear.
Scheduling handlers with a posted event
A single-threaded event loop processes a FIFO event queue and is idle at time 0 ms. Dispatch overhead is negligible, and each handler runs for the handler time shown. Four events come from outside the program. The fifth, Redraw E, is posted by Key B's handler: it is appended to the back of the queue at the moment that handler finishes. Complete the table, giving every time in milliseconds as a whole number.
Splitting a long handler to bound click latency
A spreadsheet app runs on a single-threaded event loop with one FIFO queue; a running handler is never interrupted. At t=0t = 0t=0 ms, with the queue otherwise empty, an import event starts a job that processes 12,000 rows at 0.05 ms per row. At t=130t = 130t=130 ms the user clicks a button whose handler takes 8 ms. Posting an event takes negligible time.
Version A processes all 12,000 rows in a single handler call.
Version B processes 1,000 rows per handler call, then, as its last action, posts a continue event to the back of the queue; the continue handler does the next 1,000 rows the same way until all rows are done.