What is an Event Loop?

Card Puncher Data Processing

About

An Event loop 1) is a thread that waits for and dispatches events or messages that may contains:

  • data
  • or runnable code

An event loop is also known as:

  • message dispatcher,
  • message loop,
  • message pump,
  • or run loop

When the event loop in a event-driven system is the entry point, it's referred to:

  • the main loop
  • or main event loop.

because it runs on the main thread.

They are responsible for:

  • executing the code,
  • collecting and processing events,
  • and executing queued sub-tasks

Each event shall be processed in a reasonable amount of time to not block the event loop. This means that thread-blocking operations shall not be performed while executed on the event loop, exactly like processing events in a graphical user interface (e.g., freezing a Java / Swing interface by doing a slow network request).

An events can be :

It is almost non-blocking (of kernel threads) allowing it to handle a lot of concurrency (e.g. handle many connections, or messages) using a very small number of kernel threads, which allows it to scale very well.

Features

Event loops are used to implement:

Example

This is how asynchrony is implemented:

Implementation

The event loop got its name from it's minimal implementation:

while (queue.waitForMessage()) {
  queue.processNextMessage()
}

where queue

The event loop works by:

  • making a request to some internal or external event provider (that generally blocks the request until an event has arrived),
  • then calls the relevant event handler (“dispatches the event”).





Discover More
Java Conceptuel Diagram
Java - Vert.X Framework

Vert.x is not a framework but a toolkit: the core library defines the fundamental APIs for writing asynchronous networked applications, and then you can pick the useful modules for your application (e.g.,...
Javascript - Asynchrony

This page is Asynchrony in javascript. Asynchrony is not only critical to the performance of our applications, but it’s also increasingly becoming the critical factor in writability and maintainability....
Process States
OS - Main Thread

OS The main thread is another name for a process because every process has at least one thread, the main thread. The term main means that this is the entry point. From the application programmer's...
Multi Threading Puppies
Process - (Kernel) Thread (Lightweight processes - LWP)

A thread is a child unit of execution of a process (also known as the ). Threads exist within a process. A process acts as a container for threads. A process is a unit of resources, while a thread is...
Event Centric Thinking
Stream - Processing

Stream processing is the reactive design of data processing. Something happened (Event), Subscribe to it (Streams) Streaming Processing is also known as : Incremental Processing. or reactive...
Java Conceptuel Diagram
Vert.x - Event-loop

This page is th event loop in Vertx. Vert.x calls handlers by using a thread known as an event loop. The event loop is created with a Vertx object. Every event loop is attached to a thread. ...
Card Puncher Data Processing
What is Reactive Programming?

Reactive Programming is an event-driven programmation based on the observer / subscriber pattern via an event loop. They are reactive implementing a reactor pattern. It means that: functions create...
Card Puncher Data Processing
What is an Event-Driven processing model?

An event-oriented model is a system that is driven from an event point of view, not from a state point of view. Ie: when an event occurs, transformation happens to the entities that change their...
What is the Javascript Thread Model?

JavaScript is a single-thread event-loop based model. By default, all JavaScript applications (whether they be Node.js, Deno, or Bun) run on a single operating system thread. They won’t benefit from...
Card Puncher Data Processing
What is the Reactor Pattern?

The reactor pattern explained



Share this page:
Follow us:
Task Runner