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

!15672 翻译完成:14696 add concurrent(worker and taskpool) doc

Merge pull request !15672 from wusongqing/TR14696
......@@ -64,6 +64,7 @@ function func(args) {
console.log("func: " + args);
return args;
}
let task = new taskpool.Task(func, "this is my first Task");
```
......@@ -116,7 +117,12 @@ function func(args) {
return args;
}
let value = taskpool.execute(func, 100);
async function taskpoolTest() {
let value = await taskpool.execute(func, 100);
console.log("taskpool result: " + value);
}
taskpoolTest();
```
## taskpool.execute
......@@ -158,8 +164,14 @@ function func(args) {
console.log("func: " + args);
return args;
}
let task = new taskpool.Task(func, "this is my first Task");
let value = taskpool.execute(task);
async function taskpoolTest() {
let task = new taskpool.Task(func, 100);
let value = await taskpool.execute(task);
console.log("taskpool result: " + value);
}
taskpoolTest();
```
## taskpool.cancel
......@@ -193,9 +205,14 @@ function func(args) {
console.log("func: " + args);
return args;
}
let task = new taskpool.Task(func, "this is first Task");
let value = taskpool.execute(task);
taskpool.cancel(task);
async function taskpoolTest() {
let task = new taskpool.Task(func, 100);
let value = await taskpool.execute(task);
taskpool.cancel(task);
}
taskpoolTest();
```
## Additional Information
......@@ -214,10 +231,18 @@ function func(args) {
return args;
}
let task = new taskpool.Task(func, "create task, then execute");
let val1 = taskpool.execute(task);
async function taskpoolTest() {
// taskpool.execute(task)
let task = new taskpool.Task(func, "create task, then execute");
let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1);
let val2 = taskpool.execute(func, "execute task by func");
// taskpool.execute(function)
let val2 = await taskpool.execute(func, "execute task by func");
console.log("taskpool.execute(function) result: " + val2);
}
taskpoolTest();
```
```js
......@@ -226,7 +251,7 @@ let val2 = taskpool.execute(func, "execute task by func");
// b.ts
export var c = 2000;
// a.ts
// a.ts (in the same directory as b.ts)
import { c } from './b'
function test(a) {
......@@ -236,8 +261,16 @@ function test(a) {
return a;
}
let task = new taskpool.Task(test, "create task, then execute");
let val1 = taskpool.execute(task);
async function taskpoolTest() {
// taskpool.execute(task)
let task = new taskpool.Task(test, "create task, then execute");
let val1 = await taskpool.execute(task);
console.log("taskpool.execute(task) result: " + val1);
// taskpool.execute(function)
let val2 = await taskpool.execute(test, "execute task by func");
console.log("taskpool.execute(function) result: " + val2);
}
let val2 = taskpool.execute(test, "execute task by func");
taskpoolTest();
```
......@@ -82,24 +82,24 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
import worker from '@ohos.worker';
// Create a Worker instance.
// In the FA model, the worker script directory and pages directory are at the same level.
// In the FA model, the workers directory is at the same level as the pages directory in the entry module.
const workerFAModel01 = new worker.ThreadWorker("workers/worker.js", {name:"first worker in FA model"});
// In the FA model, the worker script directory and pages directory are at different levels.
// In the FA model, the workers directory is at the same level as the parent directory of the pages directory in the entry module.
const workerFAModel02 = new worker.ThreadWorker("../workers/worker.js");
// In the stage model, the worker script directory and pages directory are at the same level.
// In the stage model, the workers directory is at the same level as the pages directory in the entry module.
const workerStageModel01 = new worker.ThreadWorker('entry/ets/workers/worker.ts', {name:"first worker in Stage model"});
// In the stage model, the worker script directory and pages directory are at different levels.
// In the stage model, the workers directory is at the same level as the parent directory of the pages directory in the entry module.
const workerStageModel02 = new worker.ThreadWorker('entry/ets/pages/workers/worker.ts');
// For the script URL "entry/ets/workers/worker.ts" in the stage model:
// entry is the value of the name attribute under module in the module.json5 file.
// ets indicates the programming language in use.
// entry is the value of the name attribute under module in the module.json5 file, and ets indicates the programming language in use.
// The script URL is related to the level of the workers directory where the worker file is located and is irrelevant to the file where the new worker is located.
```
Depending on whether the worker script directory and **pages** directory are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file.
Depending on whether the **workers** directory and **pages** directory are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file.
(1) The worker script directory and **pages** directory are at the same level.
(1) The **workers** directory and **pages** directory are at the same level.
In the FA model:
......@@ -125,7 +125,7 @@ In the stage model:
}
```
(2) The worker script directory and **pages** directory are at different levels.
(2) The **workers** directory and **pages** directory are at different levels.
In the FA model:
......@@ -586,6 +586,15 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```js
const workerInstance = new worker.ThreadWorker("workers/worker.js");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```js
const workerInstance = new worker.ThreadWorker("workers/worker.js");
// Usage 1:
workerInstance.on("alert_on", (e)=>{
console.log("alert listener callback");
......@@ -602,7 +611,7 @@ workerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is n
// The event listener created by on will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
// The event listener created by addEventListener will be always valid and will not be proactively deleted.
// The event listener created by addEventListener will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
......@@ -699,7 +708,7 @@ Removes an event listener for the worker thread. This API provides the same func
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------------- | ---- | ---------------------------- |
| type | string | Yes | Type of the event for which the event listener is to be removed. |
| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when an event of the specified type occurs. |
| callback | [WorkerEventListener](#workereventlistener9) | No| Callback to invoke when an event of the specified type occurs. Callback of the event listener to remove.|
**Error codes**
......@@ -752,6 +761,15 @@ For details about the error codes, see [Utils Error Codes](../errorcodes/errorco
```js
const workerInstance = new worker.ThreadWorker("workers/worker.js");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```js
const workerInstance = new worker.ThreadWorker("workers/worker.js");
// Usage 1:
workerInstance.on("alert_on", (e)=>{
console.log("alert listener callback");
......@@ -768,7 +786,7 @@ workerInstance.dispatchEvent({type:"alert_once", timeStamp:0});// timeStamp is n
// The event listener created by on will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_on", timeStamp:0});
// The event listener created by addEventListener will not be proactively deleted.
// The event listener created by addEventListener will be always valid and will not be proactively deleted.
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
workerInstance.dispatchEvent({type:"alert_add", timeStamp:0});
......@@ -1168,23 +1186,23 @@ A constructor used to create a **Worker** instance.
import worker from '@ohos.worker';
// Create a Worker instance.
// In the FA model, the worker script directory and pages directory are at the same level.
// In the FA model, the workers directory is at the same level as the pages directory.
const workerFAModel01 = new worker.Worker("workers/worker.js", {name:"first worker in FA model"});
// In the FA model, the worker script directory and pages directory are at different levels.
// In the FA model, the workers directory is at the same level as the parent directory of the pages directory.
const workerFAModel02 = new worker.Worker("../workers/worker.js");
// In the stage model, the worker script directory and pages directory are at the same level.
// In the stage model, the workers directory is at the same level as the pages directory.
const workerStageModel01 = new worker.Worker('entry/ets/workers/worker.ts', {name:"first worker in Stage model"});
// In the stage model, the worker script directory and pages directory are at different levels.
// In the stage model, the workers directory is at the same level as the child directory of the pages directory.
const workerStageModel02 = new worker.Worker('entry/ets/pages/workers/worker.ts');
// For the script URL "entry/ets/workers/worker.ts" in the stage model:
// entry is the value of the name attribute under module in the module.json5 file.
// ets indicates the programming language in use.
```
Depending on whether the worker script directory and **pages** directory are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file.
Depending on whether the **workers** directory and **pages** directory are at the same level, you may need to configure the **buildOption** attribute in the **build-profile.json5** file.
(1) The worker script directory and **pages** directory are at the same level.
(1) The **workers** directory and **pages** directory are at the same level.
In the FA model:
......@@ -1207,7 +1225,7 @@ In the stage model:
}
}
```
(2) The worker script directory and **pages** directory are at different levels.
(2) The **workers** directory and **pages** directory are at different levels.
In the FA model:
```json
......@@ -1594,6 +1612,14 @@ Dispatches the event defined for the worker thread.
```js
const workerInstance = new worker.Worker("workers/worker.js");
workerInstance.dispatchEvent({type:"eventType", timeStamp:0}); // timeStamp is not supported yet.
```
The **dispatchEvent** API can be used together with the **on**, **once**, and **addEventListener** APIs. The sample code is as follows:
```js
const workerInstance = new worker.Worker("workers/worker.js");
// Usage 1:
workerInstance.on("alert_on", (e)=>{
console.log("alert listener callback");
......@@ -2056,7 +2082,7 @@ Each actor concurrently processes tasks of the main thread. For each actor, ther
### FA Model
```js
// main.js (The following assumes that the worker script directory and pages directory are at the same level.)
// main.js (The following assumes that the workers directory and pages directory are at the same level.)
import worker from '@ohos.worker';
// Create a Worker instance in the main thread.
const workerInstance = new worker.ThreadWorker("workers/worker.ts");
......@@ -2121,7 +2147,7 @@ Configuration of the **build-profile.json5** file:
```
### Stage Model
```js
// main.js (The following assumes that the worker script directory and pages directory are at different levels.)
// main.js (The following assumes that the workers directory and pages directory are at different levels.)
import worker from '@ohos.worker';
// Create a Worker instance in the main thread.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册