Asynchronous Waiting

To address timing control issues in multithreaded tasks, ArkTS has introduced asynchronous waiting and notification capabilities. The ConditionVariable object supports pass-by-reference across threads.

ArkTS, which supports asynchronous operations, now provides the capability for asynchronous tasks to wait and be awakened. When a wake-up notification is received or the waiting times out, the asynchronous task continues executing.

NOTE

Asynchronous methods must be marked as async, and the caller must use the await keyword to ensure correct timing.

Usage Example

The following example demonstrates how to control asynchronous task waiting and awakening using Sendable objects across threads.

import { ArkTSUtils, taskpool } from '@kit.ArkTS';

@Concurrent
function notifyAll(conditionVariable: ArkTSUtils.locks.ConditionVariable) {
  conditionVariable.notifyAll();
}

@Concurrent
function notifyOne(conditionVariable: ArkTSUtils.locks.ConditionVariable) {
  conditionVariable.notifyOne();
}

@Concurrent
async function wait(conditionVariable: ArkTSUtils.locks.ConditionVariable) {
  await conditionVariable.wait().then(() => {
    console.info(`TaskPool Thread Wait: success`);
  });
}

@Concurrent
async function waitFor(conditionVariable: ArkTSUtils.locks.ConditionVariable) {
  await conditionVariable.waitFor(3000).then(() => {
    console.info(`TaskPool Thread WaitFor: success`);
  });
}

@Entry
@Component
struct Index {
  @State message: string | ResourceStr = $r('app.string.AsyncButton');

  build() {
    Row() {
      Column() {
        Button(this.message)
          .fontSize(25)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            // Create a conditionVariable object.
            const conditionVariable: ArkTSUtils.locks.ConditionVariable = new ArkTSUtils.locks.ConditionVariable();
            // Pass the conditionVariable object to the wait thread.
            taskpool.execute(wait, conditionVariable);
            // Pass the conditionVariable object to the notifyAll thread to wake up the wait thread. The log information "TaskPool Thread Wait: success" is displayed.
            taskpool.execute(notifyAll, conditionVariable);
            // Pass the conditionVariable object to the waitFor thread.
            taskpool.execute(waitFor, conditionVariable);
            // Pass the conditionVariable object to the notifyOne thread to wake up the waitFor thread. The log information "TaskPool Thread WaitFor: success" is displayed.
            taskpool.execute(notifyOne, conditionVariable);

            // Create a conditionVariable object with a name.
            const conditionVariableRequest: ArkTSUtils.locks.ConditionVariable =
              ArkTSUtils.locks.ConditionVariable.request('Request1');
            // Pass the conditionVariableRequest object to the wait thread.
            taskpool.execute(wait, conditionVariableRequest);
            // Pass the conditionVariableRequest object to the notifyAll thread to wake up the wait thread. The log information "TaskPool Thread Wait: success" is displayed.
            taskpool.execute(notifyAll, conditionVariableRequest);
            // Pass the conditionVariableRequest object to the waitFor thread.
            taskpool.execute(waitFor, conditionVariableRequest);
            // Pass the conditionVariableRequest object to the notifyOne thread to wake up the waitFor thread. The log information "TaskPool Thread WaitFor: success" is displayed.
            taskpool.execute(notifyOne, conditionVariableRequest);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}