worker.isMainThread
添加於:v10.5.0
如果此代碼未在
線程內運行,則為 Worker
true
。
const { Worker, isMainThread } = require('node:worker_threads');
if (isMainThread) {
// This re-loads the current file inside a Worker instance.
new Worker(__filename);
} else {
console.log('Inside Worker!');
console.log(isMainThread); // Prints 'false'.
}
相關用法
- Node.js worker.receiveMessageOnPort(port)用法及代碼示例
- Node.js worker.markAsUntransferable(object)用法及代碼示例
- Node.js worker.workerData用法及代碼示例
- Node.js worker.SHARE_ENV用法及代碼示例
- Node.js worker.parentPort用法及代碼示例
- Node.js worker.getEnvironmentData(key)用法及代碼示例
- Node.js writeStream.getColorDepth()用法及代碼示例
- Node.js writeStream.cursorTo()用法及代碼示例
- Node.js writable._construct(callback)用法及代碼示例
- Node.js writeStream.clearLine()用法及代碼示例
- Node.js writeStream.moveCursor()用法及代碼示例
- Node.js writeStream.getWindowSize()用法及代碼示例
- Node.js writeStream.columns用法及代碼示例
- Node.js writeStream.clearScreenDown()用法及代碼示例
- Node.js writeStream.isTTY用法及代碼示例
- Node.js writeStream.hasColors()用法及代碼示例
- Node.js writeStream.rows用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2.Http2ServerRequest request.url用法及代碼示例
- Node.js request.socket用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js tlsSocket.authorized用法及代碼示例
- Node.js zlib.deflateRaw()用法及代碼示例
- Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
- Node.js Console用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 worker.isMainThread。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。