doc,worker: revise worker_threads.md

Rewrite worker_threads introductory material focusing on simpler and
shorter sentences.

PR-URL: https://github.com/nodejs/node/pull/25402
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Rich Trott 2019-01-08 15:18:19 -08:00
parent 906db44ccd
commit 1415d579a9

View File

@ -4,21 +4,20 @@
> Stability: 1 - Experimental > Stability: 1 - Experimental
The `worker` module provides a way to create multiple environments running The `worker_threads` module enables the use of threads with message channels
on independent threads, and to create message channels between them. It between them. To access it:
can be accessed using:
```js ```js
const worker = require('worker_threads'); const worker = require('worker_threads');
``` ```
Workers are useful for performing CPU-intensive JavaScript operations; do not Workers (threads) are useful for performing CPU-intensive JavaScript operations.
use them for I/O, since Node.jss built-in mechanisms for performing operations They will not help much with I/O-intensive work. Node.jss built-in asynchronous
asynchronously already treat it more efficiently than Worker threads can. I/O operations are more efficient than Workers can be.
Workers, unlike child processes or when using the `cluster` module, can also Unlike `child_process` or `cluster`, `worker_threads` can share memory. They do
share memory efficiently by transferring `ArrayBuffer` instances or sharing so by transferring `ArrayBuffer` instances or sharing `SharedArrayBuffer`
`SharedArrayBuffer` instances between them. instances.
```js ```js
const { const {
@ -46,10 +45,9 @@ if (isMainThread) {
} }
``` ```
Note that this example spawns a Worker thread for each `parse` call. The above example spawns a Worker thread for each `parse()` call. In actual
In practice, it is strongly recommended to use a pool of Workers for these practice, use a pool of Workers instead for these kinds of tasks. Otherwise, the
kinds of tasks, since the overhead of creating Workers would likely exceed the overhead of creating Workers would likely exceed their benefit.
benefit of handing the work off to it.
## worker.isMainThread ## worker.isMainThread
<!-- YAML <!-- YAML