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

# Import Attributes

## `runtime: 'shared'`

通过 **Import Attributes** 显式声明某些模块可以在主线程和后台线程之间共享。

这仅用于配合 **[Main Thread Functions](/next/zh/react/main-thread-script.md)** 使用。详情请参考 [Cross-thread Shared Modules](/next/zh/react/main-thread-script.md#cross-thread-shared-modules)。

`import` 语句 `with` 子句中的 `runtime` 属性指定了所导入模块的运行时行为。当设置为 `'shared'` 时，该模块会被标记为跨线程共享。

### Syntax

```javascript
import { ... } from "module-name" with { runtime: 'shared' };
import "module-name" with { runtime: 'shared' };
```

### Parameters

- `runtime`: `'shared'`

### Examples

```javascript
import { func } from './utils.js' with { runtime: 'shared' };

function mainThreadFunction() {
  'main thread';
  func(); // Can be called within a main thread function
}
```

### Precautions

1. **静态分析**：此功能依赖编译期静态分析。只有通过 `import ... with { runtime: 'shared' }` 直接导入的标识符才会被识别为 shared。
2. **标识符传递**：如果一个 shared 的导入函数被赋值给另一个变量，新变量会失去 shared 特性，无法在主线程函数中直接调用（除非再次将其包装为主线程函数）。
3. **主线程限制**：尽管 shared module 中的代码可以运行在主线程上，但除非显式标记为 `'main thread'`，否则它不会自动获得主线程上下文（例如无法直接使用 `MainThreadRef`）。
4. **状态隔离**：Shared Module 中的变量和状态在两个线程之间是**完全隔离**的，各自拥有独立的实例。在一个线程中修改状态不会影响另一个线程。
