void TaskManager::distributeTask(ID taskId){ qDebug() << tr("distributeTask()")<<QThread::currentThreadId(); //查找可用线程 WorkerThread * thread = mWorkThreadPool.findAvaiableTcpThread(); if(thread == NULL){ if(mWorkThreadPool.getThreadSize()>=mWorkThreadPool.getMaxThreadSize()){ qDebug() << tr("Error: No avaiable thread for use. The thread pool is full, and size is:")<<mWorkThreadPool.getThreadSize(); return; } else{ //线程池未超出,创建新线程。 thread = new WorkerThread(); //把这个线程的事件,放在自己线程处理。 thread->moveToThread(thread); if(thread != NULL){ mWorkThreadPool.addNewWorkThread(thread); } else{ qDebug() << tr("Error: No avaiable thread for use, fail to add new thread."); return; } } } thread->setWorkListener(this); thread->start(); thread->assignTask(tasks[taskId]); }