未验证 提交 7192afcc 编写于 作者: O openharmony_ci 提交者: Gitee

!19204 翻译完成:18599+18504+18877+18953 几个API少量刷新

Merge pull request !19204 from wusongqing/zh20230605-3.2release
...@@ -4,7 +4,7 @@ The **AbilityDelegatorRegistry** module provides APIs for storing the global reg ...@@ -4,7 +4,7 @@ The **AbilityDelegatorRegistry** module provides APIs for storing the global reg
> **NOTE** > **NOTE**
> >
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityDelegatorRegistry](js-apis-app-ability-abilityDelegatorRegistry.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
......
...@@ -4,7 +4,7 @@ The **AbilityManager** module provides APIs for obtaining, adding, and modifying ...@@ -4,7 +4,7 @@ The **AbilityManager** module provides APIs for obtaining, adding, and modifying
> **NOTE** > **NOTE**
> >
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.abilityManager](js-apis-app-ability-abilityManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> The APIs of this module are system APIs and cannot be called by third-party applications. > The APIs of this module are system APIs and cannot be called by third-party applications.
## Modules to Import ## Modules to Import
......
...@@ -4,7 +4,7 @@ The **missionManager** module provides APIs to lock, unlock, and clear missions, ...@@ -4,7 +4,7 @@ The **missionManager** module provides APIs to lock, unlock, and clear missions,
> **NOTE** > **NOTE**
> >
> The APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. > The APIs of this module are supported since API version 8 and deprecated since API version 9. You are advised to use [@ohos.app.ability.missionManager](js-apis-app-ability-missionManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import ## Modules to Import
......
...@@ -40,6 +40,8 @@ Creates the context based on the bundle name. ...@@ -40,6 +40,8 @@ Creates the context based on the bundle name.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core **System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
...@@ -102,6 +104,8 @@ Creates the context based on the bundle name and module name. ...@@ -102,6 +104,8 @@ Creates the context based on the bundle name and module name.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core **System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters** **Parameters**
| Name | Type | Mandatory | Description | | Name | Type | Mandatory | Description |
......
...@@ -77,7 +77,6 @@ let media = mediaLibrary.getMediaLibrary(); ...@@ -77,7 +77,6 @@ let media = mediaLibrary.getMediaLibrary();
### getFileAssets<sup>7+</sup> ### getFileAssets<sup>7+</sup>
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains file assets (also called files). This API uses an asynchronous callback to return the result. Obtains file assets (also called files). This API uses an asynchronous callback to return the result.
...@@ -105,7 +104,7 @@ async function example() { ...@@ -105,7 +104,7 @@ async function example() {
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
}; };
// Obtain the files in asynchronous callback mode. // Obtain the files in asynchronous callback mode.
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => { media.getFileAssets(imagesFetchOp, async (error, fetchFileResult) => {
// Check whether the result set of the obtained files is undefined. If yes, the API call fails. // Check whether the result set of the obtained files is undefined. If yes, the API call fails.
if (fetchFileResult == undefined) { if (fetchFileResult == undefined) {
console.error('get fetchFileResult failed with error: ' + error); console.error('get fetchFileResult failed with error: ' + error);
...@@ -124,8 +123,8 @@ async function example() { ...@@ -124,8 +123,8 @@ async function example() {
return; return;
} }
console.info('Get fetchFileResult successfully, count: ' + count); console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in asynchronous callback mode. // Obtain the first file in the result set in asynchronous callback mode. If there are a large number of files, use getAllObject instead.
fetchFileResult.getFirstObject((error, fileAsset) => { fetchFileResult.getFirstObject(async (error, fileAsset) => {
// Check whether the first file is undefined. If yes, the API call fails. // Check whether the first file is undefined. If yes, the API call fails.
if (fileAsset == undefined) { if (fileAsset == undefined) {
console.error('get first object failed with error: ' + error); console.error('get first object failed with error: ' + error);
...@@ -178,7 +177,7 @@ async function example() { ...@@ -178,7 +177,7 @@ async function example() {
selectionArgs: [imageType.toString()], selectionArgs: [imageType.toString()],
}; };
// Obtain the files in promise mode. // Obtain the files in promise mode.
media.getFileAssets(imagesFetchOp).then((fetchFileResult) => { media.getFileAssets(imagesFetchOp).then(async (fetchFileResult) => {
// Obtain the total number of files in the result set. // Obtain the total number of files in the result set.
const count = fetchFileResult.getCount(); const count = fetchFileResult.getCount();
// Check whether the number is less than 0. If yes, the API call fails. // Check whether the number is less than 0. If yes, the API call fails.
...@@ -192,8 +191,8 @@ async function example() { ...@@ -192,8 +191,8 @@ async function example() {
return; return;
} }
console.info('Get fetchFileResult successfully, count: ' + count); console.info('Get fetchFileResult successfully, count: ' + count);
// Obtain the first file in the result set in promise mode. // Obtain the first file in the result set in promise mode. If there are a large number of files, use getAllObject instead.
fetchFileResult.getFirstObject().then((fileAsset) => { fetchFileResult.getFirstObject().then(async (fileAsset) => {
console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName); console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
// Call getNextObject to obtain the next file until the last one. // Call getNextObject to obtain the next file until the last one.
for (let i = 1; i < count; i++) { for (let i = 1; i < count; i++) {
...@@ -514,9 +513,10 @@ Obtains the albums. This API uses an asynchronous callback to return the result. ...@@ -514,9 +513,10 @@ Obtains the albums. This API uses an asynchronous callback to return the result.
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => { media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
if (albumList != undefined) { if (albumList != undefined) {
...@@ -554,9 +554,10 @@ Obtains the albums. This API uses a promise to return the result. ...@@ -554,9 +554,10 @@ Obtains the albums. This API uses a promise to return the result.
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => { media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
console.info('getAlbums successfully: ' + JSON.stringify(albumList)); console.info('getAlbums successfully: ' + JSON.stringify(albumList));
...@@ -2021,7 +2022,7 @@ async function example() { ...@@ -2021,7 +2022,7 @@ async function example() {
### getNextObject<sup>7+</sup> ### getNextObject<sup>7+</sup>
getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result. Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result.
> **NOTE** > **NOTE**
...@@ -2049,7 +2050,8 @@ async function example() { ...@@ -2049,7 +2050,8 @@ async function example() {
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject(); let fileAsset = await fetchFileResult.getFirstObject();
if (!fileAsset.isAfterLast) { console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject((error, fileAsset) => { fetchFileResult.getNextObject((error, fileAsset) => {
if (error) { if (error) {
console.error('fetchFileResult getNextObject failed with error: ' + error); console.error('fetchFileResult getNextObject failed with error: ' + error);
...@@ -2065,7 +2067,7 @@ async function example() { ...@@ -2065,7 +2067,7 @@ async function example() {
### getNextObject<sup>7+</sup> ### getNextObject<sup>7+</sup>
getNextObject(): Promise&lt;FileAsset&gt; getNextObject(): Promise&lt;FileAsset&gt;
Obtains the next file asset in the result set. This API uses a promise to return the result. Obtains the next file asset in the result set. This API uses a promise to return the result.
> **NOTE** > **NOTE**
...@@ -2093,7 +2095,8 @@ async function example() { ...@@ -2093,7 +2095,8 @@ async function example() {
}; };
let fetchFileResult = await media.getFileAssets(getImageOp); let fetchFileResult = await media.getFileAssets(getImageOp);
let fileAsset = await fetchFileResult.getFirstObject(); let fileAsset = await fetchFileResult.getFirstObject();
if (!fileAsset.isAfterLast) { console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
if (!fetchFileResult.isAfterLast()) {
fetchFileResult.getNextObject().then((fileAsset) => { fetchFileResult.getNextObject().then((fileAsset) => {
console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName); console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
fetchFileResult.close(); fetchFileResult.close();
...@@ -2369,9 +2372,10 @@ Commits the modification in the album attributes to the database. This API uses ...@@ -2369,9 +2372,10 @@ Commits the modification in the album attributes to the database. This API uses
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
const albumList = await media.getAlbums(AlbumNoArgsfetchOp); const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0]; const album = albumList[0];
...@@ -2406,9 +2410,10 @@ Commits the modification in the album attributes to the database. This API uses ...@@ -2406,9 +2410,10 @@ Commits the modification in the album attributes to the database. This API uses
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
const albumList = await media.getAlbums(AlbumNoArgsfetchOp); const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0]; const album = albumList[0];
...@@ -2423,6 +2428,47 @@ async function example() { ...@@ -2423,6 +2428,47 @@ async function example() {
### getFileAssets<sup>7+</sup> ### getFileAssets<sup>7+</sup>
getFileAssets(callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains the file assets in this album. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.READ_MEDIA
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback used to return the file assets.|
**Example**
```js
async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = {
selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs:['New Album 1'],
};
// Obtain the albums that meet the retrieval options and return the album list.
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
// Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album.
album.getFileAssets((error, fetchFileResult) => {
if (error) {
console.error('album getFileAssets failed with error: ' + error);
return;
}
let count = fetchFileResult.getCount();
console.info('album getFileAssets successfully, count: ' + count);
fetchFileResult.close();
});
}
```
### getFileAssets<sup>7+</sup>
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
Obtains the file assets in this album. This API uses an asynchronous callback to return the result. Obtains the file assets in this album. This API uses an asynchronous callback to return the result.
...@@ -2442,9 +2488,10 @@ Obtains the file assets in this album. This API uses an asynchronous callback to ...@@ -2442,9 +2488,10 @@ Obtains the file assets in this album. This API uses an asynchronous callback to
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
let fileNoArgsfetchOp = { let fileNoArgsfetchOp = {
selections: '', selections: '',
...@@ -2492,9 +2539,10 @@ Obtains the file assets in this album. This API uses a promise to return the res ...@@ -2492,9 +2539,10 @@ Obtains the file assets in this album. This API uses a promise to return the res
```js ```js
async function example() { async function example() {
// To obtain the file assets in an album, you must preset the album and resources. The sample code below presets 'New Album 1'.
let AlbumNoArgsfetchOp = { let AlbumNoArgsfetchOp = {
selections: '', selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
selectionArgs: [], selectionArgs:['New Album 1'],
}; };
let fileNoArgsfetchOp = { let fileNoArgsfetchOp = {
selections: '', selections: '',
......
# @ohos.taskpool (Using the Task Pool) # @ohos.taskpool (Starting the Task Pool)
The task pool provides a multi-thread running environment for applications. It helps reduce resource consumption and improve system performance. It also frees you from caring about the lifecycle of thread instances. You can use the **TaskPool** APIs to create background tasks and perform operations on them, for example, executing or canceling a task. Theoretically, you can create an unlimited number of tasks, but this is not recommended for memory considerations. In addition, you are not advised performing blocking operations in a task, especially indefinite blocking. Long-time blocking operations occupy worker threads and may block other task scheduling, adversely affecting your application performance. The task pool provides a multi-thread running environment for applications. It helps reduce resource consumption and improve system performance. It also frees you from caring about the lifecycle of thread instances. You can use the **TaskPool** APIs to create background tasks and perform operations on them, for example, executing or canceling a task. Theoretically, you can create an unlimited number of tasks, but this is not recommended for memory considerations. In addition, you are not advised performing blocking operations in a task, especially indefinite blocking. Long-time blocking operations occupy worker threads and may block other task scheduling, adversely affecting your application performance.
...@@ -60,12 +60,12 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco ...@@ -60,12 +60,12 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts ```ts
@Concurrent @Concurrent
function func(args) { function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
let task = new taskpool.Task(func, "this is my first Task"); let task = new taskpool.Task(printArgs, "this is my first Task");
``` ```
### Attributes ### Attributes
...@@ -112,17 +112,17 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco ...@@ -112,17 +112,17 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts ```ts
@Concurrent @Concurrent
function func(args) { function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolExecute() {
let value = await taskpool.execute(func, 100); let value = await taskpool.execute(printArgs, 100);
console.log("taskpool result: " + value); console.log("taskpool result: " + value);
} }
taskpoolTest(); taskpoolExecute();
``` ```
## taskpool.execute ## taskpool.execute
...@@ -136,7 +136,7 @@ Places a task in the internal task queue of the task pool. The task will be dist ...@@ -136,7 +136,7 @@ Places a task in the internal task queue of the task pool. The task will be dist
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | --------------------- | ---- | ---------------------------------------- | | -------- | --------------------- | ---- | ------------------------------------ |
| task | [Task](#task) | Yes | Task to be executed. | | task | [Task](#task) | Yes | Task to be executed. |
| priority | [Priority](#priority) | No | Priority of the task (not supported yet).| | priority | [Priority](#priority) | No | Priority of the task (not supported yet).|
...@@ -160,18 +160,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco ...@@ -160,18 +160,18 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```ts ```ts
@Concurrent @Concurrent
function func(args) { function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolExecute() {
let task = new taskpool.Task(func, 100); let task = new taskpool.Task(printArgs, 100);
let value = await taskpool.execute(task); let value = await taskpool.execute(task);
console.log("taskpool result: " + value); console.log("taskpool result: " + value);
} }
taskpoolTest(); taskpoolExecute();
``` ```
## taskpool.cancel ## taskpool.cancel
...@@ -200,14 +200,14 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco ...@@ -200,14 +200,14 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
**Example of successful task cancellation** **Example of successful task cancellation**
```ts ```ts
function func(args) { @Concurrent
"use concurrent"; function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolCancel() {
let task = new taskpool.Task(func, 100); let task = new taskpool.Task(printArgs, 100);
taskpool.execute(task); taskpool.execute(task);
try { try {
taskpool.cancel(task); taskpool.cancel(task);
...@@ -216,20 +216,20 @@ async function taskpoolTest() { ...@@ -216,20 +216,20 @@ async function taskpoolTest() {
} }
} }
taskpoolTest(); taskpoolCancel();
``` ```
**Example of a failure to cancel a task that has been executed** **Example of a failure to cancel a task that has been executed**
```ts ```ts
function func(args) { @Concurrent
"use concurrent"; function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolCancel() {
let task = new taskpool.Task(func, 100); let task = new taskpool.Task(printArgs, 100);
let value = taskpool.execute(task); let value = taskpool.execute(task);
let start = new Date().getTime(); let start = new Date().getTime();
while (new Date().getTime() - start < 1000) {// Wait for 1s to ensure that the task has been executed. while (new Date().getTime() - start < 1000) {// Wait for 1s to ensure that the task has been executed.
...@@ -243,25 +243,25 @@ async function taskpoolTest() { ...@@ -243,25 +243,25 @@ async function taskpoolTest() {
} }
} }
taskpoolTest(); taskpoolCancel();
``` ```
**Example of a failure to cancel an ongoing task** **Example of a failure to cancel an ongoing task**
```ts ```ts
function func(args) { @Concurrent
"use concurrent"; function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolCancel() {
let task1 = new taskpool.Task(func, 100); let task1 = new taskpool.Task(printArgs, 100);
let task2 = new taskpool.Task(func, 200); let task2 = new taskpool.Task(printArgs, 200);
let task3 = new taskpool.Task(func, 300); let task3 = new taskpool.Task(printArgs, 300);
let task4 = new taskpool.Task(func, 400); let task4 = new taskpool.Task(printArgs, 400);
let task5 = new taskpool.Task(func, 500); let task5 = new taskpool.Task(printArgs, 500);
let task6 = new taskpool.Task(func, 600); let task6 = new taskpool.Task(printArgs, 600);
let res1 = taskpool.execute(task1); let res1 = taskpool.execute(task1);
let res2 = taskpool.execute(task2); let res2 = taskpool.execute(task2);
...@@ -276,7 +276,7 @@ async function taskpoolTest() { ...@@ -276,7 +276,7 @@ async function taskpoolTest() {
} }
} }
taskpoolTest(); taskpoolCancel();
``` ```
## Additional Information ## Additional Information
...@@ -288,7 +288,7 @@ The following sequenceable data types are supported: All Primitive Type (excludi ...@@ -288,7 +288,7 @@ The following sequenceable data types are supported: All Primitive Type (excludi
- The task pool APIs can be used only in the module with **compileMode** set to **esmodule** in the stage model. To check the **compileMode** setting of a module, open the **build-profile.json5** file of the module and check for **"compileMode": "esmodule"** under **buildOption**. - The task pool APIs can be used only in the module with **compileMode** set to **esmodule** in the stage model. To check the **compileMode** setting of a module, open the **build-profile.json5** file of the module and check for **"compileMode": "esmodule"** under **buildOption**.
- A task in the task pool can reference only variables passed in by input parameters or imported variables, rather than closure variables. The decorator **@Concurrent** is used to intercept unsupported variables. - A task in the task pool can reference only variables passed in by input parameters or imported variables, rather than closure variables. The decorator **@Concurrent** is used to intercept unsupported variables.
- A task in the task pool supports only common functions or async functions, rather than class member functions or anonymous functions. The decorator **@Concurrent** is used to intercept unsupported functions. - A task in the task pool supports only common functions or async functions, rather than class member functions or anonymous functions. The decorator **@Concurrent** is used to intercept unsupported functions.
- The decorator **@Concurrent** can be used only in the .ets file. To create a task in the task pool in the .ts file, use the statement **use concurrent**. - The decorator **@Concurrent** can be used only in .ets files.
### Using the Task Pool in Simple Mode ### Using the Task Pool in Simple Mode
...@@ -297,14 +297,14 @@ The following sequenceable data types are supported: All Primitive Type (excludi ...@@ -297,14 +297,14 @@ The following sequenceable data types are supported: All Primitive Type (excludi
```ts ```ts
// Common functions are supported, and variables passed in by input parameters are also supported. // Common functions are supported, and variables passed in by input parameters are also supported.
@Concurrent @Concurrent
function func(args) { function printArgs(args) {
console.log("func: " + args); console.log("printArgs: " + args);
return args; return args;
} }
async function taskpoolTest() { async function taskpoolExecute() {
// taskpool.execute(task) // taskpool.execute(task)
let task = new taskpool.Task(func, "create task, then execute"); let task = new taskpool.Task(printArgs, "create task, then execute");
let val1 = await taskpool.execute(task); let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1); console.log("taskpool.execute(task) result: " + val1);
...@@ -313,7 +313,7 @@ async function taskpoolTest() { ...@@ -313,7 +313,7 @@ async function taskpoolTest() {
console.log("taskpool.execute(function) result: " + val2); console.log("taskpool.execute(function) result: " + val2);
} }
taskpoolTest(); taskpoolExecute();
``` ```
**Example 2** **Example 2**
...@@ -328,24 +328,24 @@ export var c = 2000; ...@@ -328,24 +328,24 @@ export var c = 2000;
import { c } from "./b"; import { c } from "./b";
@Concurrent @Concurrent
function test(a) { function printArgs(a) {
console.log(a); console.log(a);
console.log(c); console.log(c);
return a; return a;
} }
async function taskpoolTest() { async function taskpoolExecute() {
// taskpool.execute(task) // taskpool.execute(task)
let task = new taskpool.Task(test, "create task, then execute"); let task = new taskpool.Task(printArgs, "create task, then execute");
let val1 = await taskpool.execute(task); let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1); console.log("taskpool.execute(task) result: " + val1);
// taskpool.execute(function) // taskpool.execute(function)
let val2 = await taskpool.execute(test, "execute task by func"); let val2 = await taskpool.execute(printArgs, "execute task by func");
console.log("taskpool.execute(function) result: " + val2); console.log("taskpool.execute(function) result: " + val2);
} }
taskpoolTest(); taskpoolExecute();
``` ```
**Example 3** **Example 3**
...@@ -353,57 +353,52 @@ taskpoolTest(); ...@@ -353,57 +353,52 @@ taskpoolTest();
```ts ```ts
// The async functions are supported. // The async functions are supported.
@Concurrent @Concurrent
async function task() { async function delayExcute() {
let ret = await Promise.all([ let ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved")) new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]); ]);
return ret; return ret;
} }
async function taskpoolTest() { async function taskpoolExecute() {
taskpool.execute(task).then((result) => { taskpool.execute(delayExcute).then((result) => {
console.log("TaskPoolTest task result: " + result); console.log("TaskPoolTest task result: " + result);
}); });
} }
taskpoolTest(); taskpoolExecute();
``` ```
**Example 4** **Example 4**
```ts ```ts
// Use use concurrent to create a task in the task pool in the .ts file. // c.ets
// c.ts @Concurrent
function test1(n) { function strSort(inPutArr) {
"use concurrent" let newArr = inPutArr.sort();
return n; return newArr;
} }
export async function taskpoolTest1() { export async function func1() {
console.log("taskpoolTest1 start"); console.log("taskpoolTest start");
var task = new taskpool.Task(test1, 100); let strArray = ['c test string', 'b test string', 'a test string'];
var task = new taskpool.Task(strSort, strArray);
var result = await taskpool.execute(task); var result = await taskpool.execute(task);
console.log("taskpoolTest1 result:" + result); console.log("func1 result:" + result);
} }
async function test2() { export async function func2() {
"use concurrent"
var ret = await Promise.all([
new Promise(resolve => setTimeout(resolve, 1000, "resolved"))
]);
return ret;
}
export async function taskpoolTest2() {
console.log("taskpoolTest2 start"); console.log("taskpoolTest2 start");
taskpool.execute(test2).then((result) => { let strArray = ['c test string', 'b test string', 'a test string'];
console.log("TaskPoolTest2 result: " + result); taskpool.execute(strSort, strArray).then((result) => {
console.log("func2 result: " + result);
}); });
} }
``` ```
```ts ```ts
/ / a.ets (in the same directory as c.ts) / / a.ets (in the same directory as c.ets)
import { taskpoolTest1, taskpoolTest2 } from "./c"; import { taskpoolTest1, taskpoolTest2 } from "./c";
taskpoolTest1(); func1();
taskpoolTest2(); func2();
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册