ThreadPool 类
const { Worker,parentPort,isMainThread } = require('worker_threads')
//主线程
if(isMainThread){
class ThreadPool {
size = 5;
queue = [];
workerGroup = [];
free=0;
maxFree=2;
monitor=null;
constructor(size) {
this.size = size;
}
//初始化子线程
init(){
for (let i = 0; i < this.size; i++) {
this.workerGroup.push({
id: i,
status: false,
worker: new Worker(__filename)
});
this.workerGroup[i].worker.on("message", (message) => {
if (message === 'end') {
this.workerGroup[i].status = false;
this.check();
}
});
}
this.monitor=setInterval(()=>{
if(this.isFree()){