继承QRunnable。和QThread使用一样,线程类继承于QRunnable。
重写run函数。和QThread一样,需要重写run函数,run是一个纯虚函数,必须重写。
使用QThreadPool启动线程
class PThreadRunnable : public QRunnable
{
public:
explicit PThreadRunnable();
~PThreadRunnable();
void run();
};
PThreadRunnable::PThreadRunnable()
{
}
PThreadRunnable::~PThreadRunnable()
{
qDebug() << __FUNCTION__;
}
void PThreadRunnable::run()
{
qDebug() << __FUNCTION__ << QThread::currentThreadId();
QThread::msleep(1000);
}
PThreadRunnable * subThread = new PThreadRunnable();
QThreadPool * threadPool = new QThreadPool();
threadPool->setMaxThreadCount(2);
threadPool->start(subThread);
threadPool->waitForDone();