import { WorkerThread } from './workerthread' ; import { WorkerTask } from './workertask' ; export class ThreadPool { private taskQueue = [ ] ; private workerQueue = [ ] ; private poolSize : number ; constructor ( size ) { this . poolSize = size ; for ( let i = 0 ; i < this . poolSize ; i ++ ) { this . workerQueue . push ( new WorkerThread ( this ) ) ; } } public addWorkerTask ( workerTask ) { if ( this . workerQueue . length > 0 ) { const workerThread = this . workerQueue . shift ( ) ; workerThread . run ( workerTask ) ; } else { this . taskQueue . push ( workerTask ) ; } } public freeWorkerThread = function ( workerThread ) { if ( this . taskQueue . length > 0 ) { const workerTask = this . taskQueue . shift ( ) ; workerThread . run ( workerTask ) ; } else { this . taskQueue . push ( workerThread ) ; } } ; }