> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Threads: where your code runs

Lynx runs JavaScript on two threads. AngularLynx runs your app on one of them, and uses the other to reach APIs your app's thread doesn't have. You can write most components without thinking about this. It matters for a few APIs that exist on only one thread.

## Two threads instead of one

In a browser, your JavaScript shares one thread with layout and painting. A long task blocks the page.

Lynx gives JavaScript two separate threads:

- The [**main thread**](/guide/glossary.md#main-thread) creates and updates the native UI, and receives touches first. It has a small set of APIs.
- The [**background thread**](/guide/glossary.md#background-thread) is a fuller JavaScript environment. Some APIs, such as `fetch`, exist only here.

The threads don't share memory. They can only send each other messages, and every message is copied, which takes time.

## Where AngularLynx runs your code

Your Angular app runs on the main thread. The background thread doesn't load it: it loads AngularLynx's side of each bridge and your [background functions](/api/main-thread/background-fn.md), and runs only what the main thread asks of it.

```mermaid
flowchart TB
    subgraph MT["Main thread"]
        direction TB
        M1["Your Angular app"] --> M2["Native UI you see"]
        M3["Taps, input, scroll"] --> M4["Your event handlers"]
    end
    subgraph BG["Background thread"]
        direction TB
        B1["Your background functions"]
        B2["Background-only APIs<br/>such as fetch"]
    end
    MT <-->|"messages (copied)"| BG
```

- **Your app draws the UI on the main thread.** Its templates become the native elements on screen.
- **Your event handlers run on the main thread**, right when the native event fires. That's why a tap gets a response without waiting for the other thread.
- **The background thread runs calls for your app.** Some AngularLynx services, such as [`LynxLogger`](/guide/debugging.md) and [`LynxNativeModule`](/api/angular-lynx/host/native-module.md), send calls there for you. Your own code gets there through a [background function](/api/main-thread/background-fn.md).

Constructors, `ngOnInit` and effects run once, like in a browser.

## The phone's UI thread

iOS and Android draw the screen on a thread of their own, the [**UI thread**](/guide/glossary.md#ui-thread). In the apps `ng build --platform ios|android` makes, the main thread isn't that thread: Lynx runs it, and your app, on a thread of its own.

So while your app is busy, building a big screen or running a long handler, the phone keeps going:

- A `<scroll-view>` you flicked keeps scrolling.
- Text fields and web views keep taking touches and typing.

What waits for your app is what Lynx draws from your templates: the next screen, a changed style, and Lynx's animations (CSS transitions, `@keyframes` and `element.animate()`), which run on the main thread too. Keep handlers short, and send heavy work to the [background thread](#apis-that-live-on-one-thread).

[Lynx Explorer](/guide/glossary.md#lynx-explorer) runs the main thread on the UI thread. There, a busy app freezes scrolling too, so check how a heavy screen feels in a build of your own app.

## Event handlers

**Inside an event handler, do what you'd do on the web.** Change signals, navigate, open overlays and start animations, straight away.

```typescript
import { Component, inject, signal } from '@angular/core';
import { Router } from '@angular/router';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-item',
  imports: [LYNX_ELEMENTS],
  template: `
    <view (bindtap)="open()">
      <text>{{ label() }}</text>
    </view>
  `,
})
export class Item {
  readonly label = signal('Open details');
  readonly #router = inject(Router);

  open(): void {
    this.label.set('Opening…');
    this.#router.navigate(['/details']);
  }
}
```

- **Angular updates the screen after the handler returns.** Setting a signal marks the view as changed, and the router builds the next screen a moment later, as on the web.
- **You don't need `setTimeout`.** Older versions of this guide said to wrap navigation, overlays and animations in it. Measured on Lynx 4.1 on iOS, Android and the web preview, a handler can create and remove elements, flush them to the screen and navigate without a crash. On iOS and Android, so can a [gesture](/guide/gestures/index.md) callback.

## APIs that live on one thread

| You want to                                 | Thread it needs | What to do                                                                                                                                                      |
| ------------------------------------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Update the UI                               | Main            | Change signals. Angular does the rest                                                                                                                           |
| Make network requests                       | Background      | Use `HttpClient` or `fetch` from anywhere. AngularLynx sends the requests to the background thread. See [HTTP requests](/guide/http.md)                         |
| Move an element on every frame, e.g. a drag | Main            | Use a [main-thread script](/guide/main-thread/index.md)                                                                                                         |
| Load a font at runtime                      | Background      | Call `LynxFont.addFont()` from anywhere. AngularLynx sends it to the background thread. See [Custom fonts](/guide/custom-fonts/index.md#load-a-font-at-runtime) |
| Call your host app's native code            | Background      | Use [`LynxNativeModule`](/api/angular-lynx/host/native-module.md). AngularLynx sends the calls to the background thread                                         |

## Related

- [How a Lynx app runs](/guide/concepts/how-it-runs.md)
- [Signals and change detection](/guide/concepts/signals.md)
- [Renderer internals](/guide/renderer-architecture.md): how the app starts on each thread, in detail.
