1# Asynchronous Waiting 2 3To address timing control issues in multithreaded tasks, ArkTS has introduced asynchronous waiting and notification capabilities. The [ConditionVariable](../reference/apis-arkts/js-apis-arkts-utils.md#conditionvariable18) object supports pass-by-reference across threads. 4 5ArkTS, 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. 6 7> **NOTE** 8> 9> Methods using asynchronous waiting must be marked as **async**, and the caller must use the **await** keyword to ensure correct timing. 10 11## Usage Example 12 13The following example demonstrates how to control asynchronous task waiting and awakening using [Sendable](arkts-sendable.md) objects across threads. 14 15```ts 16import { ArkTSUtils, taskpool } from '@kit.ArkTS'; 17 18@Concurrent 19function notifyAll(conditionVariable: ArkTSUtils.locks.ConditionVariable) { 20 conditionVariable.notifyAll(); 21} 22 23@Concurrent 24function notifyOne(conditionVariable: ArkTSUtils.locks.ConditionVariable) { 25 conditionVariable.notifyOne(); 26} 27 28@Concurrent 29async function wait(conditionVariable: ArkTSUtils.locks.ConditionVariable) { 30 await conditionVariable.wait().then(() => { 31 console.log(`TaskPool Thread Wait: success`); 32 }); 33} 34 35@Concurrent 36async function waitFor(conditionVariable: ArkTSUtils.locks.ConditionVariable) { 37 await conditionVariable.waitFor(3000).then(() => { 38 console.log(`TaskPool Thread WaitFor: success`); 39 }); 40} 41 42@Entry 43@Component 44struct Index { 45 @State message: string = 'Hello World'; 46 47 build() { 48 Row() { 49 Column() { 50 Text(this.message) 51 .fontSize(50) 52 .fontWeight(FontWeight.Bold) 53 .onClick(() => { 54 // Create a conditionVariable object. 55 const conditionVariable: ArkTSUtils.locks.ConditionVariable = new ArkTSUtils.locks.ConditionVariable(); 56 // Pass the conditionVariable object to the wait thread. 57 taskpool.execute(wait, conditionVariable); 58 // Pass the conditionVariable object to the notify thread to wake up the wait thread. The log information "TaskPool Thread Wait: success" is displayed. 59 taskpool.execute(notifyAll, conditionVariable); 60 // Pass the conditionVariable object to the waitFor thread. 61 taskpool.execute(waitFor, conditionVariable); 62 // Pass the conditionVariable object to the notifyOne thread to wake up the waitFor thread. The log information "TaskPool Thread WaitFor: success" is displayed. 63 taskpool.execute(notifyOne, conditionVariable); 64 65 // Create a conditionVariable object with a name. 66 const conditionVariableRequest: ArkTSUtils.locks.ConditionVariable = 67 ArkTSUtils.locks.ConditionVariable.request("Request1"); 68 // Pass the conditionVariableRequest object to the wait thread. 69 taskpool.execute(wait, conditionVariableRequest); 70 // Pass the conditionVariableRequest object to the notify thread to wake up the wait thread. The log information "TaskPool Thread Wait: success" is displayed. 71 taskpool.execute(notifyAll, conditionVariableRequest); 72 // Pass the conditionVariableRequest object to the waitFor thread. 73 taskpool.execute(waitFor, conditionVariableRequest); 74 // Pass the conditionVariableRequest object to the notifyOne thread to wake up the waitFor thread. The log information "TaskPool Thread WaitFor: success" is displayed. 75 taskpool.execute(notifyOne, conditionVariableRequest); 76 }) 77 } 78 .width('100%') 79 } 80 .height('100%') 81 } 82} 83``` 84