worker.isMainThread
如果此代码未在 Worker 线程中运行,则为 true。
🌐 Is true if this code is not running inside of a Worker thread.
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'.
}